0

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

2 Answers 2

0

Instead of

if (is_admin())
add_action('admin_menu', 'add_custom_box');

use

add_action('add_meta_boxes', 'add_custom_box');

The 'add_meta_boxes' hook is the correct one to add meta boxes. Other than that, as you mentioned correctly change the 'post' to '$my-custom-post-type' and register the category taxonomy for that post type:

register_taxonomy_for_object_type( 'category', '$my-custom-post-type' )
0

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'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.