Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you so much! I have yet to start learning about the WP core, so much of this is still a blur to me, especially the super globals part. This was very helpful. –  Bobby Jul 27 '13 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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