WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Edit as requested in the comments here is my jquery:

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

BR & Thanks, mybecks

share|improve this question
up vote 3 down vote accepted

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 ) ) {

share|improve this answer
An additional note: If you want it to appear on selection, you will need to do that via jQuery, the way this answer stands, it will take a save then a page reload to get the box to appear. Good answer though, +1 – mor7ifer Jan 31 '12 at 11:16
Thanks, forget to mention that. – ungestaltbar Jan 31 '12 at 11:19
Thanks that is exactly what I need. Now adding the jQuery part, to make it smoother. :) – mybecks Jan 31 '12 at 13:57
cool, would be nice if you can post your solution for the jquery part. – ungestaltbar Jan 31 '12 at 14:21
@ungestaltbar i've updated my question with the jquery part. hope that helps. – mybecks Mar 8 '13 at 7:40

Your Answer


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.