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'd like to add a meta box to all my pages (not posts) but it looks like I am only able to add it for a specific post based on the codex?

add_meta_box( $id, $title, $callback, $page, $context = 'advanced', $priority = 'default', $callback_args = null );
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this. This is part of some code I use.

 // Add meta boxes to Page
  add_meta_box( 'RelatedImage', __('Related images'), 'related_images_meta_box', 'page', 'normal', 'high' );

  function related_images_meta_box($object) { 
    $nonce = wp_create_nonce(plugin_basename( __FILE__ ));
  ?>
    <input type="hidden" name="wp_filebrowser_nonce" id="wp_filebrowser_nonce" value="<?php echo $nonce; ?>" />
    <table>
      <tr>
        <td>
          <label for="related_image" class="">Related image</label>
        </td>
        <td>
          <input type="text" id="related_image" name="related_image"  value="<?php echo 'your meta property' ?>" size="40" />  
        <td>
      </tr>
    </table>
  <?php }
share|improve this answer
    
This only add to the "page" post type. –  kaiser Mar 9 '12 at 2:01
2  
Isn't that what the OP is asking for (to add a meta box to pages, but not to (blog) posts)? Or am I misreading the question? –  Chip Bennett Mar 9 '12 at 2:10
    
@Steven...yup that's what I was asking for! –  redconservatory Mar 9 '12 at 14:25

Wordpress has the following post types by default, with your custom post types add at the end of the array:

Array
(
    [0] => post
    [1] => page
    [2] => attachment
    [3] => revision
    [4] => nav_menu_item
    [5] => ...all your custom post types
)

So you can loop through them and simply add a meta box to every post type. Note: This won't work for attachment, revision and nav_menu_item, so you want to skip them:

function wpse44962_add_meta_boxes()
{
    foreach ( array_keys( $GLOBALS['wp_post_types'] ) as $post_type )
    {
        // Skip:
        if ( in_array( $post_type, array( 'attachment', 'revision', 'nav_menu_item' ) ) )
            continue;

        // You'll have to set $id, $title, $callback yourself:
        add_meta_box( $id, $title, $callback, $post_type, 'advanced', 'default' );
    }
}
add_action( 'add_meta_boxes', 'wpse44962_add_meta_boxes' );
share|improve this answer
    
thanks , exactly what I needed –  paul Mar 23 '13 at 18:38

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.