Can I change a custom post type label from a child theme?

I’m developing a child theme of a premium template, this comes with a custom post type with the label name of “Projects” but I’d like to change it to something else, I know that if I go to the functions.php file of the main theme I can change it easily but I’d like to change it from my child theme so I don’t have to edit any of the original files, is it possible?

Thanks in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is a global array $wp_post_types. You can change $wp_post_types[$post_type]->labels after the parent theme has set the CPT.

So … if the parent theme registers the CPT on 'init' like this:

add_action( 'init', 'register_my_cpt', 12 );

Then you need a later priority:

add_action( 'init', 'change_cpt_labels', 13 );

… or a later hook. I would use wp_loaded:

add_action( 'wp_loaded', 'change_cpt_labels' );

Example for custom post type place changed to location

add_action( 'wp_loaded', 'wpse_19240_change_place_labels', 20 );

function wpse_19240_change_place_labels()
{
    $p_object = get_post_type_object( 'place' );

    if ( ! $p_object )
        return FALSE;

    // see get_post_type_labels()
    $p_object->labels->name               = 'Locations';
    $p_object->labels->singular_name      = 'Location';
    $p_object->labels->add_new            = 'Add location';
    $p_object->labels->add_new_item       = 'Add new location';
    $p_object->labels->all_items          = 'All locations';
    $p_object->labels->edit_item          = 'Edit location';
    $p_object->labels->name_admin_bar     = 'Location';
    $p_object->labels->menu_name          = 'Location';
    $p_object->labels->new_item           = 'New location';
    $p_object->labels->not_found          = 'No locations found';
    $p_object->labels->not_found_in_trash = 'No locations found in trash';
    $p_object->labels->search_items       = 'Search locations';
    $p_object->labels->view_item          = 'View location';

    return TRUE;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x