Show add_meta_box by selecting a specific category

In my plugin I want to display an add_meta_box under the WYSISWYG in the new post page, which is no problem and works fine.
But the box should only appear when a specific category (example: category with id = 5) is selected, otherwise it is not visible.

How can this be achieved?

Read More

Edit as requested in the comments here is my jquery:

$script = "
            <script type='text/javascript'>
                jQuery(document).ready(function($) {
                    $('#metabox_sectionid').hide();
                    $('#in-category-".CATEGORY."').is(':checked') ? $('#metabox_sectionid').show() : $('#emetabox_sectionid').hide();
                    $('#in-category-".CATEGORY."').click(function() {
                        $('#metabox_sectionid').toggle(this.checked);
                    });
                });
            </script>";
        echo $script;

BR & Thanks,
mybecks

Related posts

Leave a Reply

1 comment

  1. You could try this:
    get the ID’s of the attached categories and wrap your add_meta_box function inside a simple test against your defined cat ID.

    add_action('add_meta_boxes', 'is_in_cat_example');
    
    function is_in_cat_example() {
        global $post;
        $category = '5'; // can be an array of ID's as well
    
        if ( in_category( $category, $post ) ) {
        add_meta_box(...);
            }
    
    }