validate a metabox based on the category that is selected

Ok Let’s say I have 2 Categories. If a user select category A, they have to fill in metabox A and similarly for category B, it’s metabox B. So my question is, I want to do a validate check, whereby if the user has checked category A, then it is only metabox A that has been filled in, and the same applies for category B. I know JQuery is my best bet – but not too sure how to go about it or how to call the function when clicking the publish button.

Related posts

Leave a Reply

3 comments

  1. Apologies for the delay – what I did was basically created a .js file and places it within my theme folder – for example themes/nameoftheme/custom/checkcode.js

    Then using jquery – I created my code – here’s a snippet:

    jQuery(‘#in-category-6’).click(function(){
    jQuery(‘#metabox_one’).toggle(this.checked);
    jQuery(‘#metabox_two’).hide();
    jQuery(‘#metabox_three’).hide();
    jQuery(‘#in-category-3’).attr(‘checked’, this.checked);
    jQuery(‘#in-category-1’).attr(‘checked’, false);
    jQuery(‘#in-category-4’).attr(‘checked’, false);
    jQuery(‘#in-category-5’).attr(‘checked’, false);
    jQuery(‘#in-category-7’).attr(‘checked’, false);
    jQuery(‘#in-category-8’).attr(‘checked’, false);
    jQuery(‘#in-category-9’).attr(‘checked’, false)
    });

    It seems pretty explanatory but fyi
    in-category – refers to the category selected in the category box.
    attr – refers to attribute and as it’s a checkbox – hence checked.
    when catgeory-6 is selected, the metaboxes defined by the name is either hidden or not.

    After this – we now need to call this the javascript file – and this is done by going to the functions.php file of your theme.

    here you put –

    wp_enqueue_script(‘myscript’, ‘/wp-content/themes/nameoftheme/custom/checkbox.js’);

    fyi wp_enqueue_script is the bit that calls the script when the page is loaded.

    I hope this is clear for people.