1

I have a problem with a WordPress hook. I want to call an action in another actions callback, but it doesn't seem to work. I want to call add_meta_tag action only if the page is saved. This is what I have:

function saveCustomField($post_id)
{
    add_action( 'wp_head', 'add_meta_tag' );
}
add_action( 'save_post', 'saveCustomField' );

function add_meta_tag(){
    echo "TEST";
}

How can I get the above to work properly?

3
  • 2
    save_post is called only in the admin pages and not on public side then saveCustomField is never called with this code. what do you want to display on public side ?
    – mmm
    Commented Oct 18, 2017 at 21:26
  • Other than the comment above, it's not recommended to do this. There is a chance that you get stuck in an infinite loop.
    – Johansson
    Commented Oct 18, 2017 at 23:50
  • @mmm and @JackJohansson: I respectfully disagree with both of you, that save_post is fired only on admin side. That's a myth! @JackJohansson: there is no chance of getting stuck in an infinite loop, if done correctly, by removing your action within the callback function. Commented Oct 19, 2017 at 2:20

4 Answers 4

2

You're thinking about this entirely wrong. A meta tag isn't something you add when a post gets saved, it's something that gets added to the output when a post is viewed.

So instead of trying to hook the action inside save_post, you hook it on every page load, and inside the hook you check if your custom field exists on the post being viewed. If it is, you output the tag.

function wpse_283352_add_meta_tag() {
    if ( is_singular() {
        $post_id = get_queried_object_id();
        $meta = get_post_meta( $post_id, '_my_custom_field', true );

        if ( $meta ) {
            echo '<meta name="my_custom_field" content="' . esc_attr( $meta ) . '">';
        }
    }
}
add_action( 'wp_head', 'wpse_283352_add_meta_tag' );

That function just goes in your plugin file/functions file, not inside any other hook.

0

The save_post hook is fired after the data is saved to the database - whenever post or page is created or updated - only on admin pages.

The wp_head hook fires when wp_head() function is run and this do not happen on admin pages.

This won't work. Where and when you want to add the meta tag?

0

The short answer: you can't.

The code is fine, but there is a flaw in logic. wp_head action is set too late. Headers are already sent, therefore wp_head will never fire! This code below, will prove two points. First: save_post will fire on both, admin pages, and on front end, as well. Second: it's easy to prevent an infinite loop in callback function.

In functions.php:

function saveCustomField($post_id) {

    // to prevent an infinite loop
    remove_action('save_post', 'saveCustomField', 10);

    // to prove that function was called
    error_log('I am here to add action');

    add_action('wp_head', 'add_meta_tag');
}
add_action('save_post', 'saveCustomField');

function add_meta_tag(){
    error_log('TEST');
    //echo "TEST";
}

In my page template ( front end ):

wp_update_post(array('ID' => 79, 'post_title' => 'My Current Test',));

In error_log:

[19-Oct-2017 02:57:08 UTC] I am here to add action

My post's title had been updated, save_post was fired, and wp_head wasn't.

1
  • Sorry, new on wordpress. How can I make to use save_post on a post? Commented Nov 19, 2019 at 21:15
0

I want to add the meta tag when publish/update button is fired. I have a checkbox on the admin page that add/remove meta tag. For info, it's intended for a plugin.

function saveCustomField($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
}

if (
    !isset($_POST['my_custom_nonce']) ||
    !wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id)
) {
    return;
}

if (!current_user_can('edit_post', $post_id)) {
    return;
}

if (isset($_POST['_my_custom_field'])) {
    update_post_meta($post_id, '_my_custom_field', $_POST['_my_custom_field']);
    // here
    // >>>>> add_action('wp_head', 'add_meta_tag');
} else {
    delete_post_meta($post_id, '_my_custom_field');
}

}

add_action( 'save_post', 'saveCustomField' );

function add_meta_tag(){
    echo "TEST";
}
1
  • your wp_head will not fire. Headers already sent. Commented Oct 19, 2017 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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