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 am trying to create a custom meta box for multiple custom post types in WordPress without creating duplicate code.

Currently I have:

add_meta_box('adv_form_box1', 'Advanced Form', 'adv_form1', 'custom-post-type1', 'side', 'high');
add_meta_box('adv_form_box2', 'Advanced Form', 'adv_form2', 'custom-post-type2', 'side', 'high');
add_meta_box('adv_form_box3', 'Advanced Form', 'adv_form3', 'custom-post-type3', 'side', 'high');

function adv_form1() {
global $post;
$custom = get_post_custom($post->ID);
$filename = $custom['filename'][0]; ?>
<label>Filename:</label> (this field is required)<br/><input name="filename" size="70" value="<?php echo $filename; ?>" />
<?php }

function adv_form2() {
global $post;
$custom = get_post_custom($post->ID);
$filename = $custom['filename'][0]; ?>
<label>Filename:</label> (this field is required)<br/><input name="filename" size="70" value="<?php echo $filename; ?>" />
<?php }

function adv_form3() {
global $post;
$custom = get_post_custom($post->ID);
$filename = $custom['filename'][0]; ?>
<label>Filename:</label> (this field is required)<br/><input name="filename" size="70" value="<?php echo $filename; ?>" />
<?php }

I feel as though I am creating a bunch of extra un-needed code. Is there anyway to set the meta box as "global" to be used in all post-types?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

In addition to the other answers, you can further improve it by looping through an array of types instead of manually adding a new add_meta_box() line each time you add a new type.

$postTypes = array( 'custom-post-type1', 'custom-post-type2', 'custom-post-type3' );
foreach( $postTypes as $pt )
    add_meta_box( 'adv_form_box1', 'Advanced Form', 'adv_form1', $pt, 'side', 'high' );
share|improve this answer
add comment

You are creating a bunch of extra work.

Rather than creating a separate callback function for each post type, just use the same callback. So:

add_meta_box('adv_form_box', 'Advanced Form', 'adv_form', 'custom-post-type1', 'side', 'high');
add_meta_box('adv_form_box', 'Advanced Form', 'adv_form', 'custom-post-type2', 'side', 'high');
add_meta_box('adv_form_box', 'Advanced Form', 'adv_form', 'custom-post-type3', 'side', 'high');

function adv_form() {
    global $post;
    $custom = get_post_custom($post->ID);
    $filename = $custom['filename'][0]; 
?>
<label>Filename:</label> (this field is required)<br/><input name="filename" size="70" value="<?php echo $filename; ?>" />
<?php 
}
share|improve this answer
add comment

Yes! You can use the same callback function:

add_meta_box('adv_form_box1', 'Advanced Form', 'adv_form1', 'custom-post-type1', 'side', 'high');
add_meta_box('adv_form_box2', 'Advanced Form', 'adv_form1', 'custom-post-type2', 'side', 'high');
add_meta_box('adv_form_box3', 'Advanced Form', 'adv_form1', 'custom-post-type3', 'side', 'high');

(I've kept the IDs the same, but these could be identical for each post type too).

share|improve this answer
add comment

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.