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 need to register a metabox in my WordPress custom post, but only in case the post is permanent saved in database.

What I mean, when the user click on "Add new" I don't want to display the metabox. When the user click on "Publish" after the page refresh display the metabox.

The post status doesn't matter (published, private or what ever)

Any idea ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

As an alternative to @m0r7if3r's solution, the add_meta_boxes hook optionally passes two variables, the post type and post object. You can use this to conditionally add your metabox. New posts have the post status of 'auto-draft'.

add_action( 'add_meta_boxes', 'myplugin_add_custom_box',10,2);
function myplugin_add_custom_box($post_type, $post) {
    if($post->post_status != 'auto-draft'){
        add_meta_box( 
         'myplugin_sectionid',
         __( 'My Post Section Title', 'myplugin_textdomain' ),
         'myplugin_inner_custom_box',
         'post' );
    }
}
share|improve this answer
1  
+1 really solid solution...I'd be interested to see if it outperformed mine in real-world usage. –  m0r7if3r Mar 7 '12 at 18:19

You should be able to filter the call to add_meta_box() conditionally using the $current_screen variable or get_current_screen(). The only time that the user shouldn't see the box is on post-new.php, once they leave that page, some form of saving has been done.

share|improve this answer
    
How to use the $current_screen, and the get_current_screen(). Any tutorial? I user the $current_screen with global $current_screen; echo $current_screen and I get no response, and I user the get_current_screen() and I get a PHP error, function not exists :( –  Merianos Nikos Mar 7 '12 at 18:15
1  
You can view what's in $current_screen with print_r(). It's not available until after the admin_head hook though, so make sure you're hooking into admin_head or something around there. –  m0r7if3r Mar 7 '12 at 18:17
    
Nice ! thanks ! –  Merianos Nikos Mar 7 '12 at 18:19

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.