0

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?

3 Answers 3

2

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' );
2

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 
}
1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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