1

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' );
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' );
5
  • That's not exactly true. It depends on how should this code work... – Krzysiek Dróżdż Jun 11 '18 at 16:34
  • @KrzysiekDróżdż Please explain... – Shibi Jun 11 '18 at 16:39
  • I've added an answer with my explanation :) – Krzysiek Dróżdż Jun 11 '18 at 16:42
  • @KrzysiekDróżdż I know this but it not why his code not working his echo... I know its risky to use the 10 as priority but its wasn't the problem in his code. – Shibi Jun 11 '18 at 16:45
  • It depends what the problem exactly is - I really don't believe that all that OP was trying to achieve was to echo that value ;) That's why I don't say your answer is incorrect. I just say it's not full and I'm posting my as another possible reason why it's not working like it should :) – Krzysiek Dróżdż Jun 11 '18 at 16:48
1

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.

8
  • hi, thanks for the answer, however how can i see the acf value that has been updated? – Nikko Dela Cruz Jun 12 '18 at 8:42
  • and also, i want to put a condition if the update is triggered only in users panel – Nikko Dela Cruz Jun 12 '18 at 8:48
  • Where do you want to show it? You can't echo it during save_post - it won't be shown anywhere on the website in such case... – Krzysiek Dróżdż Jun 12 '18 at 8:49
  • ok, i managed it by using debug log, my last question is how could i condition it only in users screen/panel? – Nikko Dela Cruz Jun 12 '18 at 9:01
  • @NikkoDelaCruz and do you have any post editors on front-end? Usually posts are saved only in backend by users. – Krzysiek Dróżdż Jun 12 '18 at 9:19
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.