How does the filter post_updated_messages work?

I’m following a tutorial about custom post types and I am unable to understand how some functions work.

Allow me to explain a little more.

Read More
function my_updated_messages( $messages ) {
global $post, $post_ID;
$messages['product'] = array(
    0 => '', 
    1 => sprintf( __('Product updated. <a href="%s">View product</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Product updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Product published. <a href="%s">View product</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Product saved.'),
    8 => sprintf( __('Product submitted. <a target="_blank" href="%s">Preview product</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Product scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview product</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Product draft updated. <a target="_blank" href="%s">Preview product</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages' );

I have copied part of the tutorial code snippet above. In this example, I do know that $messages contains and array which has certain post types. To allow custom messages to a custom post type, a new array is made and then returned. The function my_updated_message() is then filtered (I know what add_filter does).

Okay, the questions:

  1. When a new function has a parameter, the parameter has to be passed when the function is called right? In the case above, how does WP know that $messages is in fact, $messages from WP?
  2. Sorry for being totally unrelated to the context above, but why does var_dump($post_updated_messages); return null to me. I want to see what’s inside.

A detailed explanation or a link to any tutorial would be greatly appreciated.

Related posts

1 comment

  1. When a new function has a parameter, the parameter has to be passed
    when the function is called right? In the case above, how does WP know
    that $messages is in fact, $messages from WP?

    If it is a WordPress hook (you can add your own) running in its normal context (you can run filters anywhere if you want) then WordPress calls the function when apply_filters('hook-name', ....) runs. The parameters are passed in at that time so WordPress has control of it. It is possible to run apply_filters yourself though why you would ever need to do that with post messages I don’t know. Applying the_content filters is probably pretty common, so for example:

    // data from some external source
    $data = get_my_external_content();
    $data = apply_filters('the_content',$data);
    

    Now you’ve passed your own data through that filter. Again, the parameter is passed when apply_filters runs.

    Sorry for being totally unrelated to the context above, but why does
    var_dump($post_updated_messages); return null to me. I want to see
    what’s inside.

    post_updated_messages is a hook name, not a variable you can dump. Inside your callback var_dump($messages) will give you what you have to work with.

    If you want to see the filters and callbacks try var_dump($GLOBALS['wp_filter']);. That is an enormous array. Be warned. Not everything will show up, though. It depends on context and on what filters have and haven’t been added.

Comments are closed.