1

I'm using the Toolset Types plugin to create various custom posts and custom taxonomies.

I have 2 custom post types (Schools, and Productions) that share a common taxonomy (School Subjects). However, on the Schools edit page, I want the title of that taxonomy meta box to say "Subject Wishlist", while leaving the default title on the Productions edit page. So I can't just rename the taxonomy as a whole, because that would change it everywhere.

Any ideas? I have considered the various other answers here covering removing and re-adding metaboxes (eg this answer). I'm concerned that may not work though, since the Toolset Types plugin generates these metaboxes dynamically (I think?)...

2 Answers 2

1

There is a filter called register_taxonomy_args in WP v4.4 that's a big help here. This tested and working example assumes that the post types schools and productions have already been created.

/**
 * Filter the arguments for registering a taxonomy.
 *
 * @since 4.4.0
 *
 * @param array  $args        Array of arguments for registering a taxonomy.
 * @param string $taxonomy    Taxonomy key.
 * @param array  $object_type Array of names of object types for the taxonomy.
 */
add_filter( 'register_taxonomy_args', 'wpse238461_taxonomy_school_subjects_title_changer', 10, 3 );
function wpse238461_taxonomy_school_subjects_title_changer( $args, $taxonomy, $object_type ) {

    // We'll need to figure out what kind of post type we're looking at, and it's too early for other methods.
    if ( ! isset( $_GET['post_type'] ) || ! $_GET['post_type'] ) {
        return $args;
    }
    $post_type = $_GET['post_type'];

    /*
    // Bonus code for anyone trying to do this on regular post or page edit screens,
    // $_GET['post_type'] is not set, so do something like this instead of/in addition to the above check

    // We'll need to figure out what kind of post type we're looking at, and it's too early for other methods.
    if ( ! isset( $_GET['post'] ) || ! $_GET['post'] ) {
        return $args;
    }

    // Get the post type (post or page), bail if we can't. 
    $post_type = get_post_type( $_GET['post'] );
    if ( ! $post_type ) {
        return $args;
    }   
    */

    // Make sure we're looking at the right taxonomy since this filter runs for all of them.
    if ( 'school_subjects' !== $taxonomy ) {
        return $args;
    }

    // Check if we're viewing the appropriate post type, and modify the label.
    if ( 'schools' === $post_type ) {
        $args['labels']['name']              = _x( 'Subject Wishlists', 'taxonomy general name', 'textdomain' );

        // Alternate example for when taxonomy is using the label argument instead of the entire labels argument:
        // $args['label'] = 'Subject Wishlist';
    }

    return $args;
}

// Taxonomy registration code
add_action( 'init', 'create_taxonomy_school_subjects' );
function create_taxonomy_school_subjects() {
    register_taxonomy(
        'school_subjects',
        array( 'schools', 'productions' ),
        array(
            //'label' => __( 'School Subjects' ),
            'labels' => array(
                'name'              => _x( 'School Subjects', 'taxonomy general name', 'textdomain' ),
                'singular_name'     => _x( 'School Subject', 'taxonomy singular name', 'textdomain' ),
                'search_items'      => __( 'Search School Subjects', 'textdomain' ),
                'all_items'         => __( 'All School Subjects', 'textdomain' ),
                'parent_item'       => __( 'Parent School Subject', 'textdomain' ),
                'parent_item_colon' => __( 'Parent School Subject:', 'textdomain' ),
                'edit_item'         => __( 'Edit School Subject', 'textdomain' ),
                'update_item'       => __( 'Update School Subject', 'textdomain' ),
                'add_new_item'      => __( 'Add New School Subject', 'textdomain' ),
                'new_item_name'     => __( 'New School Subject Name', 'textdomain' ),
                'menu_name'         => __( 'School Subjects', 'textdomain' ),
            ),
            'rewrite' => array( 'slug' => 'school_subjects' ),
            'hierarchical' => true,
        )
    );
}

School - Subject Wishlists

School - Subject Wishlists

Production - School Subjects

Production - School Subjects

6
  • 1
    whatever is new today will be old tomorrow ;) Sep 9, 2016 at 23:48
  • Fair enough! Thanks to WP's commitment to backcompat, it should still work tomorrow :-P Sep 9, 2016 at 23:52
  • bigger issue is that it doesn't answer the question as it is not selective enough :( You will have the label changed on all mentions, not just the specific box Sep 9, 2016 at 23:54
  • Hrmm, yeah. Perhaps this is not the best approach. How would you do it? Also, can you be more specific, by "all mentions"? Sep 10, 2016 at 0:00
  • I've removed the other label keys, but still this may not be the best way of handling it. Sep 10, 2016 at 0:06
3

There should be no reason to use plugins for meta boxes :(. Using them means that you give away part of your ability to customize things your way.

Still, think it is safe to assume that ids are unlikely to change even if they are dynamically generated so it will be safe to just remove and re-add, or target the label with some JS code.

But.... for me all alarm bells go off when I hear "It is the same, except for that small difference, so lets just change the label at that specific point". This in many cases is just the first indication that it is not the same at all, and all you are trying to do is short cut a fuller solution. before doing any coding, you should ask yourself why is it that you want to confuse the admin by having the same information named differently when it is in the menu and when it is in the form? This sounds like at least a bad UX.

Your Answer

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

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