最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

advanced custom fields - Get ACF value after update user using save_post

programmeradmin1浏览0评论

How to get the value of ACF after clicking update user in User menu. I have the following code but it's not working:

function get_acf_value ($post_id) {
    $v = get_field('field_5b1d13fce338d', $post_id);
    echo $v;
}
add_action( 'acf/save_post', 'get_acf_value' );

How to get the value of ACF after clicking update user in User menu. I have the following code but it's not working:

function get_acf_value ($post_id) {
    $v = get_field('field_5b1d13fce338d', $post_id);
    echo $v;
}
add_action( 'acf/save_post', 'get_acf_value' );
Share Improve this question edited Jun 11, 2018 at 14:46 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jun 11, 2018 at 14:06 Nikko Dela CruzNikko Dela Cruz 1474 silver badges15 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Your code is working but its not going to print to the screen anything because save_post running in ajax. You can debug the action with the error_log().

First in the wp-config.php you need to turn debug and set it to log into file instead of display

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DEBUG_LOG', true);

It will create you a file in the folder wp-content in the name debug.log

And then you can use error_log() to debug like this:

function get_acf_value ($post_id) {
    $v = get_field('field_5b1d13fce338d', $post_id);
    error_log($v);
    // Incase it is array you can use print_r
    error_log( print_r( $v, true ) );
}
add_action( 'acf/save_post', 'get_acf_value' );

As Shibi already stated, echoing the value during save_post or acf/save_post doesn't make much sense, since you won't see much of your output. But I don't think echoing it is the thing you want to do with this value. I'm guessing it's just some way of debugging?

But, there is one more problem with your code. Most probably it will return previous value for given field. Why? ACF doc offers some explanation.

This filter allows you to hook in before or after the data has been saved. It is important to note that the get_field() function will return different values at these times (previous value / new value).

So if you'll use priority less than 10, you will get previous value for given field.

function get_acf_value ($post_id) {
    $v = get_field('field_5b1d13fce338d', $post_id);
    // $v contains old value of field 'field_5b1d13fce338d'
    echo $v;
}
add_action( 'acf/save_post', 'get_acf_value', 1 ); // <- priority is 1

And if you'll use priority greater than 10, you will get new value for that field.

function get_acf_value ($post_id) {
    $v = get_field('field_5b1d13fce338d', $post_id);
    // $v contains new value of field 'field_5b1d13fce338d'
    echo $v;
}
add_action( 'acf/save_post', 'get_acf_value', 20 ); // <- priority is 20

Using it with priority equal to 10 is a little bit risky - most probably it will get you old or new value, depending on when your hook is assigned.

Hardcoding fields IDs is considered generally bad idea, because it will work only if you export and import your fields, and you're hardcoding ACF's implementation details into your code. Much less volatile solution is to use field's name.

Let's say your field name is is_allowed_to_make_reviews. You can use the code below to hook when field is saved and do anything you need with it's value.

function update_is_allowed_to_make_reviews($user_id)
{
    if (is_admin() === false) { // if you want it to work only in wp_admin
        return;
    }

    if ($_POST['_acf_screen'] !== 'user') { // to not fire it on other screens
        return;
    }

    $value = get_field('is_allowed_to_make_reviews', $user_id);

    echo $value;
}

add_action('acf/save_post', 'update_is_allowed_to_make_reviews', 100);
发布评论

评论列表(0)

  1. 暂无评论