0

Is it possible to add a "Select All" to a custom taxonomy for my custom post type? A project I'm working on requires the option to be able to select all of the taxonomy terms. While I could go through and manually click each term, it becomes quite cumbersome when I have several hundred terms.

I haven't tried anything yet, as I'm not sure how to go about it....is it something I can add to my functions.php or is it something that can be accomplished with javascript?

5
  • Javascript. I am fairly sure that makes the question off-topic here though. – s_ha_dum Jun 30 '13 at 15:00
  • Thanks @s_ha_dum. I'll post this elsewhere, unless you're able to move it for me? – NW Tech Jun 30 '13 at 15:01
  • 1
    I think you could also do it with PHP. In my Radio Buttons for Taxonomies plugin I filtered get_terms to add a "No Term" option to the checklist. You could add an "All terms" checkbox and then process that on save post. – helgatheviking Jun 30 '13 at 15:04
  • @helgatheviking that sounds like it might work. Could you offer more information, and maybe an example of how I might accomplish this? Thanks! – NW Tech Jun 30 '13 at 15:06
  • @NWTech : no, I can't move it. Mods can though. helgatheviking is right though. You could rebuild that meta_box to have another field. The Javascript solution is going to be the simpler solution though. – s_ha_dum Jun 30 '13 at 15:07
2

You don't have to rebuild the metabox... you can just add a pseudo term via the get_terms filter. This will add an "All Terms" term to your checkbox list, (assuming a hierarchical taxonomy).

add_filter( 'get_terms', 'wpa104168_all_terms', 10, 3 );

function wpa104168_all_terms ( $terms, $taxonomies, $args ){

        if ( is_admin() && function_exists( 'get_current_screen' ) && ! is_wp_error( $screen = get_current_screen() ) && in_array( $screen->base, array( 'post', 'edit-post', 'edit' ) ) ) {

            if( in_array( 'genre', ( array ) $taxonomies ) ) {

                $all_terms = __( 'All Genres' );

                $all = (object) array( 'term_id' => 'all', 'slug' => 'all', 'name' => $all_terms, 'parent' => '0' );

                $terms['all'] = $all;
            }
        }
        return $terms;
    }

And this runs on save_post tests for the presence of that pseudo term and then assigns all the terms in that taxonomy to that particular post.

add_action( 'save_post', 'wpa104168_save_all_terms', 10, 3 );

function wpa104168_save_all_terms ( $post_id ){

// verify this came from our screen and with proper authorization.

    if ( !wp_verify_nonce( $_POST['_wpnonce'], 'update-post_' . $post_id )) {
        return $post_id;
    }

    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;

    // Check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
    }

    // OK, we're authenticated: we need to find and save the data
   if ( isset( $_POST['tax_input']['genre'] ) && is_array( $_POST['tax_input']['genre'] ) && in_array( 'all', $_POST['tax_input']['genre'] ) ){

    $args = array( 'hide_empty'    => false );
        $terms = get_terms( 'genre', $args );


        if ( ! is_wp_error( $terms ) ){

            foreach ( $terms as $term ){
                $update[] = $term->slug;
            }

            wp_set_object_terms( $post_id, $update, 'genre' );

        }

   }



    return $post_id;

}

But a scripted solution is a lot simpler. Doesn't need to be loaded on every admin page, but I'll leave that to you to sort out.

Clarification: change genrechecklist to the appropriate taxonomy name.... {$taxonomy}checklist

add_action( 'admin_print_footer_scripts', 'wpa104168_js_solution' );

function wpa104168_js_solution(){ ?>

<script type="text/javascript">

jQuery(document).ready(function($) {

    $('ul#genrechecklist').append('<li><label class="selectit"><input type="checkbox" class="toggle-all-terms"/> Check All</label>');

    $('.toggle-all-terms').on('change', function(){
        $(this).closest('ul').find(':checkbox').prop('checked', this.checked );
    });

});
</script>

<?php }
2
  • Perfect! The scripted solution worked great....it did take me a minute to figure out why it didn't work at first, until I realized I had to change 'ul#genrechecklist' to the proper ID of my list. Thanks again! – NW Tech Jun 30 '13 at 17:20
  • Forgot to point that out... good point for anyone coming after. I wanted to toggle the label between 'Check all' and 'uncheck all', but the markup is weird and I didn't feel like figuring it out. – helgatheviking Jun 30 '13 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.