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.

This might be better suited to Stack Overflow, thought I'd try here first though.

I was walking through this tutorial by Justin Tadlock and encountered the function below being used as the callback argument for the add_meta_box function. My questions concern the parameters...

function smashing_post_class_meta_box( $object, $box ) {

wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' );

$output = '<p>';
$output .= '<label for="smashing-post-class">Add a custom CSS class, which will be applied to WordPress\' post class.</label>';
$output .= '<br />';
$output .= '<input class="widefat" type="text" name="smashing-post-class" id="smashing-post-class" value="' . esc_attr( get_post_meta( $object->ID, 'smashing_post_class', true ) ) . '" size="30" />';
$output .= '</p>';

echo $output;
}

I'm assuming $object is the global $post object? Where does it come from, how does it get passed into the callback? It also seems as though $box is unused, is that some sort of PHP in/out parameter? I've seen this kind of stuff scattered around WordPress, and my PHP knowledge is limited to WP so I'm not sure what exactly I'm seeing. I did a quick search at PHP.net and around the Codex, any info would be appreciated.

share|improve this question

1 Answer 1

If you have a look at function do_meta_boxes() in wp-admin/includes/template.php then you'll see this line close to the end of the function:

call_user_func($box['callback'], $object, $box);

That calls the callback function and provides the two arguments. The $box argument holds all the information about the metabox, like ID, title, callback function.

In wp-admin/edit-form-advanced.php, which displays your post edit/create screen, you can see the do_meta_boxes calls in action, e.g.:

do_meta_boxes(null, 'advanced', $post);

So, in that case $objectwould indeed be a post object. In other cases, like the dashboard screen, $object is actually set to an empty string.

It might be worth noting that add_meta_box() just adds to the $wp_meta_boxes global. In do_meta_boxes() that global then gets looped through and the boxes are being displayed.

share|improve this answer

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.