The following code hides a meta box when certain categories are selected using jquery, in post editor. It works with post
as the custom post type. I need it to work with a custom post type called events
. The event
type uses a custom taxonomy called event categories, I want to use the ids in these categories with the event
post type. Here is my working code that works with the standard post type:
add_meta_box.php in the plugin folder:
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
'post' // posttype to add 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" />';
}
if (is_admin())
add_action('admin_menu', 'add_custom_box');
wp_enqueue_script('metabox_js', plugins_url('add_meta_box/js/metabox.js',dirname(__FILE__) ), array('jquery'));
metabox.js in pluginfolder/js/metabox.js:
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)*/
$('#categorychecklist 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, [22,23]) > -1 && $(this).is(':checked'))
{
$('#my-meta-box').show();
}
});
}
$('#categorychecklist input[type="checkbox"]').live('click', my_check_categories); // calls the function on click of category checkbox
my_check_categories(); // calls the function on load
});
I know that I need to change post
to event
in add_meta_box
, I have done this but it doesn't seem to work. Please can someone provide a bit of guidance, it would be really appreciated? Thank-you