Skip to main content
added 96 characters in body
Source Link
user28566
user28566

jQuery(function($) { function my_check_categories() { $('#my-meta-box').hide(); // intially hides the metabox

jQuery(function($)
{
    function my_check_categories()
    {
        $('#my-meta-box').hide(); // intially hides the metabox
        
        /* script to show metabox on category ids 2,3 and 4 (write the category ids in the if condition below on line 14)*/
        $('#event-categorieschecklist input[type="checkbox"]').each(function(i,e)
        {
            var id = $(this).attr('id').match(/-([0-9]*)$/i);
 
            id = (id && id[1]) ? parseInt(id[1]) : null ;
 
            if ($.inArray(id, [20,6]) > -1 && $(this).is(':checked'))
            {
                $('#my-meta-box').show();
            }
        });
    }
 
    $('#event-categorieschecklist input[type="checkbox"]').live('click', my_check_categories); // calls the function on click of category checkbox
 
    my_check_categories(); // calls the function on load
});

});

jQuery(function($) { function my_check_categories() { $('#my-meta-box').hide(); // intially hides the metabox

    /* script to show metabox on category ids 2,3 and 4 (write the category ids in the if condition below on line 14)*/
    $('#event-categorieschecklist input[type="checkbox"]').each(function(i,e)
    {
        var id = $(this).attr('id').match(/-([0-9]*)$/i);

        id = (id && id[1]) ? parseInt(id[1]) : null ;

        if ($.inArray(id, [20,6]) > -1 && $(this).is(':checked'))
        {
            $('#my-meta-box').show();
        }
    });
}

$('#event-categorieschecklist input[type="checkbox"]').live('click', my_check_categories); // calls the function on click of category checkbox

my_check_categories(); // calls the function on load

});

jQuery(function($)
{
    function my_check_categories()
    {
        $('#my-meta-box').hide(); // intially hides the metabox
        
        /* script to show metabox on category ids 2,3 and 4 (write the category ids in the if condition below on line 14)*/
        $('#event-categorieschecklist input[type="checkbox"]').each(function(i,e)
        {
            var id = $(this).attr('id').match(/-([0-9]*)$/i);
 
            id = (id && id[1]) ? parseInt(id[1]) : null ;
 
            if ($.inArray(id, [20,6]) > -1 && $(this).is(':checked'))
            {
                $('#my-meta-box').show();
            }
        });
    }
 
    $('#event-categorieschecklist input[type="checkbox"]').live('click', my_check_categories); // calls the function on click of category checkbox
 
    my_check_categories(); // calls the function on load
});
Source Link
user28566
user28566


The below code solves this problem:

metabox.js code:

jQuery(function($) { function my_check_categories() { $('#my-meta-box').hide(); // intially hides the metabox

    /* script to show metabox on category ids 2,3 and 4 (write the category ids in the if condition below on line 14)*/
    $('#event-categorieschecklist input[type="checkbox"]').each(function(i,e)
    {
        var id = $(this).attr('id').match(/-([0-9]*)$/i);

        id = (id && id[1]) ? parseInt(id[1]) : null ;

        if ($.inArray(id, [20,6]) > -1 && $(this).is(':checked'))
        {
            $('#my-meta-box').show();
        }
    });
}

$('#event-categorieschecklist input[type="checkbox"]').live('click', my_check_categories); // calls the function on click of category checkbox

my_check_categories(); // calls the function on load

});

add_meta_box.php code:

function add_custom_box() {
    add_meta_box(
        'my-meta-box', // id of metabox
        'My Custom Meta Box', //title of metabox
        'my_callback_function', // callback function that will echo the box content
        'event' // posttype to add meta box
    );
}
 
// This function echoes the content of our meta box
function my_callback_function() {
    echo '<label for="my_meta_box_text">Text</label>: <input type="text" id="my_meta_box_text" name="my_meta_box_text"  />';
}
 
// Hook the metabox
if (is_admin())
    add_action('admin_menu', 'add_custom_box');
    
// Add the js file which will contain script for show/hide metabox based on selection of categories.
wp_enqueue_script('metabox_js', plugins_url('add_meta_box/js/metabox.js',dirname(__FILE__) ), array('jquery'));