0

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 ?

2 Answers 2

5

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' );
    }
}
1
  • 1
    +1 really solid solution...I'd be interested to see if it outperformed mine in real-world usage.
    – mor7ifer
    Commented Mar 7, 2012 at 18:19
1

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.

2
  • 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 :(
    – KodeFor.Me
    Commented Mar 7, 2012 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.
    – mor7ifer
    Commented Mar 7, 2012 at 18:17

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.