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.


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.

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.