2

I recently wrote a WordPress plugin called Organize Drafts, which creates a custom taxonomy called Draft Types (using the slug lswdrafttype). This taxonomy is used to organize drafts of pages and posts, as well as (optionally) custom post types.

My plugin uses a custom meta box that displays a dropdown of draft types to choose from. I only want this meta box to appear on drafts, not on published, scheduled, or any other type of post/page.

Here's how it looks now - for a Draft Post

enter image description here

Here's how it looks now - for a Published Post

enter image description here

Current Code

Currently, my plugin checks the post_status in the function that renders the meta box, and then display a message instead of the drop down, like this:

public static function update_meta_boxes() {
    $post_types = apply_filters('lsw_default_post_types', LSW_Organize_Drafts::$post_types);
    remove_meta_box( 'tagsdiv-lswdrafttype', $post_types, 'side' );
    add_meta_box('lswdrafttype_custom', __('Draft Type', 'lsw_organize_drafts'), array('LSW_Organize_Drafts',
            'render_meta_box'), $post_types, 'side', 'core');
}

public static function render_meta_box($post) {
    if($post->post_status!=='draft') {
        printf( esc_html__( 'Not a draft' ));
        return;
    } else {
        //create dropdown form
    }
}

This is less than ideal, for 2 reasons:

  • The box still displays on published posts, and that's ugly.
  • When a new post is created, initially it has no post_status (I think prior to the first autosave) so the dropdown menu doesn't appear.

What I'm Thinking of Changing

I know that I can remove the meta box with Javascript but I don't know if that's the best option, and I also don't know if I should use Ajax to accomplish this task?

I'm thinking that when the edit post page is loaded, the ajax request would send the post_ID and then the ajax callback function would check for the post_status. If the post_status was empty or a draft, the box would be displayed, but otherwise, I'd remove it.

Are there any pitfalls in doing it this way? Is there a better solution?

3

I will suggest to check the $post object in PHP before adding meta box. And add the meta box if status is draft or auto-draft.

Consider this code

function update_meta_boxes($current_post_type, $post) {
    $post_types = apply_filters('lsw_default_post_types', LSW_Organize_Drafts::$post_types);
    remove_meta_box( 'tagsdiv-lswdrafttype', $post_types, 'side' );
    if (isset($post->post_status) && ($post->post_status == 'draft' || $post->post_status == 'auto-draft')) {
        add_meta_box('lswdrafttype_custom', __('Draft Type', 'lsw_organize_drafts'), array('LSW_Organize_Drafts','render_meta_box'), $post_types, 'side', 'core');
    }

}
add_action( 'add_meta_boxes', 'update_meta_boxes', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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