Home › Forums › General Issues › How to set default custom field value on new posts for a specific user
Maybe a function like:
add_action('wp_insert_post', 'set_default_custom_fields');
function set_default_custom_fields($post_id){
if ( $_GET['post_type'] == 'post' ) {
add_post_meta($post_id, 'Field Name', 'Field Value', true);
}
return true;
}
But with a specific user filter… Even better if this default value couldn’t be changed by that user when creating new posts.
Here is a simple example of how I went about assigning a value to a custom field automatically.
Notes:
+ I used the https://www.advancedcustomfields.com/resources/acf-prepare_field/ prepare_field filter to accomplish this.
+ I set a Rule for the Field Group so that the field only gets displayed to the User *after* the Post is Published. This satisfies the criteria for: “default value couldn’t be changed by that user when creating new posts” (I also included an optional “read only” line of code to prevent any manual editing of the field – readonly can be set conditionally as well)
+ I also used the “field key” within the filter call. You can expose the “key” from the Screen Options tab at the top of the Edit Fields page.
+ Further criteria: Only update if the field doesn’t already have a value, and only update it for a specific User.
Here’s the code to store the IP Address of the User in a custom field on-the-fly (not overly useful, but serves as an example):
<?php
function my_acf_prepare_field( $field ) {
$field['readonly'] = true; // optionally make the field read-only
if ( $field['value'] ) { return $field; } // bail if field has a value already
$for_user = 2;
$current_user_id = (int)get_current_user_id();
if ( $current_user_id != $for_user ) { return $field; } // bail if not specified user
$ip = my_get_ip_function();
if ( $ip ) {
$field['value'] = $ip;
}
return $field;
}
add_filter('acf/prepare_field/key=field_59d7dc6ac103a', 'my_acf_prepare_field');
// this functions gets the IP Address
function my_get_ip_function() {
if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
} else if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}
return $ip_address;
}
?>
Thank you for the swift answer! I limited each author to only be able to post in a certain category with a plugin, so I would rather not use IP (which may change). Setting a Rule for the Field Group will affect all the other custom fields whithin that group? If so how can only one custom field be set as default and not editable?
Setting the IP on-the-fly was just for example’s sake 🙂
Yes… that rule would affect the whole Field Group… but.. you can have multiple field groups. The other way, is to set that field to “read-only” using the filter above. You can make it read-only always, or under certain conditions.
Hope that helps!
I’m afraid I don’t have the skills/knowledge to modify that code but I will give it a shot if you guide me. Will give it a try tomorrow.
Sounds good. I do suggest that you create a custom plugin, rather than modify the functions.php file of your theme. That way… if you ever switch themes, things should continue to work as expected.
Creating a simple plugin is quite easy. I always use the free plugin Pluginception. Once active, it creates a new option under Plugins called “Create a Plugin” or something like that. Then you just have to give it a name… then Save it… Paste in the code, save again, and test.
Hope you feel confident giving this a try. Once you succeed with it your confidence will surely grow. I will keep tabs on this thread and reply at least once daily!
Ok I apologize for the late reply… what I don’t quite grasp is the function to get the ip adress. Here is the modified code I have so far:
<?php
function my_acf_prepare_field( $field ) {
$field['readonly'] = true; // optionally make the field read-only
if ( $field['value'] ) { return $field; } // bail if field has a value already
$for_user = 2;
$current_user_id = (int)get_current_user_id();
if ( $current_user_id != $for_user ) { return $field; } // bail if not specified user
$ip = my_get_field_function();
if ( $ip ) {
$field['value'] = $ip;
}
return $field;
}
add_filter('acf/prepare_field/key=HEREFIELDKEY', 'my_acf_prepare_field');
// this functions gets the custom field value
function my_get_field_function() {
if ( !empty( $field] ) ) {
$field_value = $field;
} else if ( !empty( 'HEREFIELDVALUE' ) ) {
$field_value = 'HEREFIELDVALUE';
} else {
$field_value = $_SERVER['REMOTE_ADDR'];
}
return $field_value;
}
?>
Where “2” is the user ID,
HEREFIELDKEY is the field key,
HEREFIELDVALUE is the field value intended to set for the user.
Question whats the second “else” for (for the getting the ip adress part)?
Oh and thanks for taking the time to guide me! I really hope I get to learn more php with this example and not just copy paste the code (though a part of me prefers that). Regards.
I’m afraid is not working.. wordpress is no longer loading!
Questions: where do I set the custom field name?
Oh and for the moment learning to create a plugin is beyond my learning scope right now..
I have so far:
function my_acf_prepare_field( $field ) {
$field['readonly'] = true; // optionally make the field read-only
if ( $field['Madrid'] ) { return $field; } // bail if field has a value already
$for_user = 2;
$current_user_id = (int)get_current_user_id();
if ( $current_user_id != $for_user ) { return $field; } // bail if not specified user
$ip = my_get_field_function();
if ( $ip ) {
$field['value'] = $ip;
}
return $field;
}
add_filter('acf/prepare_field/name=region', 'my_acf_prepare_field');
// this functions gets the custom field value
function my_get_field_function() {
if ( !empty( $field] ) ) {
$field_value = $field;
} else if ( !empty( 'Madrid' ) ) {
$field_value = 'Madrid';
} else {}
return $field_value;
}
Okey I feel like I’m spaming the thread, even though the topic is set as solved can I get some hints.. thanks!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.