0

I have a custom taxonomy of US states for a custom post type. Currently when adding a new post to this custom post type I have to manually check all 50 states (if applicable) is there a way to add a button inside of the custom taxonomy metabox that when pressed would check all of the boxes to assign the post to all 50 states?

TripsLeft
  • 539
  • 8
  • 24
  • Yes, this would be relatively simple with a bit of admin panel-specific jquery that programmatically checks all of the checkboxes. What have you tried so far? This would require some PHP and JS to achieve. – Ennui Dec 13 '13 at 14:12
  • Actually haven't tried anything thus far. Wasn't sure where to even begin. Didn't want to conflict with the metabox's existing functionality – TripsLeft Dec 13 '13 at 15:03
  • Look up some tutorials on how to add scripts to admin panel. All you need to do is programmatically (JS) add a button to that metabox on the edit posts page that triggers a JS function that ticks all the checkboxes for you. PHP required would be minimal just a couple lines in functions.php to add your scripts to the admin panel. – Ennui Dec 13 '13 at 15:14
  • thanks, I'll take a look. If I can figure it out I'll re-post with the solution. – TripsLeft Dec 13 '13 at 15:22

1 Answers1

1

So this is how I added it in: I added this code into functions.php:

//Add Script to CPT Page
    add_action( 'admin_print_scripts-post-new.php', 'portfolio_admin_script', 11 );
    add_action( 'admin_print_scripts-post.php', 'portfolio_admin_script', 11 );

    function portfolio_admin_script() {
    global $post_type;
    if( 'counselor' == $post_type )
    wp_enqueue_script( 'portfolio-admin-script', get_stylesheet_directory_uri() . '/js/counselor.js' );
}   
function style_state_button() {
   echo '<style type="text/css">
       #select-all-states-btn {
        margin-top: 15px;
       }

       #statesserved-adder h4 {
        display: none;
       }
     </style>';
}

add_action('admin_head', 'style_state_button');

This Calls a javascript file called counselor.js. In that file was this:

jQuery(document).ready(function() {     
jQuery('div#statesserved-adder ').prepend('<input type="button" class="button" id="select-all-states-btn" value="Select All States" />');

jQuery("#select-all-states-btn").click(function() {
    var checkBoxes = jQuery('input[name="tax_input[statesserved][]"]');
    checkBoxes.attr("checked", !checkBoxes.attr("checked"));
    }); 

});
TripsLeft
  • 539
  • 8
  • 24