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 }