0

I have a custom post named vpn. That custom post has a custom field named company_location.

Inside my functions.php file I am using the updated_post_meta hook to call a bg_update function I created.

I was expecting the function to var_dump the words it works at the top of my post edit screen after I change the company_location custom field on the post and click "update".

But nothing happens. No var_dump shows at all.

function bg_update ($meta_id, $object_id, $meta_key, $_meta_value) {
    $test = "it works";
    if ($meta_key == "company_location") {
        var_dump($test);
    }
}
add_action( 'updated_post_meta', 'bg_update', 10, 4 );

What am I doing wrong?

UPDATE:

This question here says that the post edit page redirects after loading, so you need to kill wp to see the var_dump. After playing with that I have found that this will kill wp and show the var_dump:

function bg_update ($meta_id, $object_id, $meta_key, $meta_value) {
    $test = "it works";
    var_dump($test);
    wp_die();
    if ($meta_key == "company_location") {
        // do stuff here
    }
}
add_action( 'updated_post_meta', 'bg_update', 10, 4 );

But this does nothing. WP is not killed and the var_dump is not showing:

function bg_update ($meta_id, $object_id, $meta_key, $meta_value) {
    $test = "it works";
    if ($meta_key == "company_location") {
        var_dump($test);
        wp_die();
    }
}
add_action( 'updated_post_meta', 'bg_update', 10, 4 );

So I now know that updated_post_meta is the correct hook because it is firing the function, but the problem is the IF statement is evaluating to false and therefore not running var_dump.

Why is that?

UPDATE 2:

I var_dumped $meta_key outside the IF statement and it's showing string(10) "_edit_lock"??

function bg_update ($meta_id, $object_id, $meta_key, $meta_value) {
    $test = "it works";
    var_dump($meta_key);
    wp_die();    
    if ($meta_key == "company_location") {
        // do things
    }
}
add_action( 'updated_post_meta', 'bg_update', 10, 4 );
1
  • Have you tried not using a var_dump but just die($test);?
    – Paul
    May 19 '18 at 8:16
1

I think you forgot the priority of hook and number of arguments otherwise code is perfect. If you can update post meta for custom post type then you refer this.

https://codex.wordpress.org/Plugin_API/Action_Reference/updated_(meta_type)_meta

Try Like this:

add_action( 'updated_post_meta', 'bg_update', 10, 4 );

For custom post type EX: abc

<?php add_action( "updated_{$meta_type}_meta", $function_name', 10, 4 ); ?>

<?php add_action( "updated_abc_meta", $function_name', 10, 4 ); ?>

UPDATE

function save_custom_field( $post_id, $post, $update ) {
//YOUR CODE
}
add_action( 'save_post', 'save_custom_field', 10, 3 );
10
  • Have tried this thanks, but still nothing shows for var_dump. The var_dump text does normally show on the post edit screen doesn't it?
    – TinyTiger
    May 18 '18 at 4:20
  • after var_dump write exit; and check page is break or not? May 18 '18 at 4:24
  • When I put exit; outside the IF statement it breaks the page (shows all white, but no var_dump showing). When I put exit; inside the IF statement, directly after the var_dump, everything is the same (normal edit page, with no var_dump showing).
    – TinyTiger
    May 18 '18 at 7:38
  • You can use default post type POST or any custom? May 18 '18 at 7:41
  • The custom field I'm updating is on a custom post type I created. Does that matter?
    – TinyTiger
    May 18 '18 at 7:41

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.