Jeremy Miller
Jeremy Miller

Reputation: 383

Change labels of custom post type via child theme

I have a parent wordpress theme that uses a custom post type of "portfolio" but I want to change this to "property." I'd like to have all of the contextual elements changed to show "property" like, "show properties," "add new property," "delete property," etc. I know I can do this by updating the parent theme, but I'd prefer not to if I can avoid it.

I found this thread: https://wordpress.stackexchange.com/questions/19240/can-i-change-a-custom-post-type-label-from-a-child-theme

Unfortunately, I'm not PHP savvy enough to write my own function and change this. Can someone help me out with this? I'm kind of stuck.

I have a feeling this is something simple for a php dev. I just don't know how to do it.

Upvotes: 1

Views: 5429

Answers (2)

timocouckuyt
timocouckuyt

Reputation: 121

In reply to Jeremy Miller's answer, you don't have to loop trough the admin menu to change the labels. The complete list of the labels is infact:

'name'
'singular_name'
'menu_name'
'name_admin_bar'
'all_items'
'add_new'
'add_new_item'
'edit_item'
'new_item'
'view_item'
'search_items'
'not_found'
'not_found_in_trash'
'parent_item_colon'

Just take a look at https://codex.wordpress.org/Function_Reference/register_post_type#Arguments

Since you can specify 'menu_name', there's no use in adding an extra hook and looping trough the menu items as specified by Jeremy.

Upvotes: 2

brasofilo
brasofilo

Reputation: 26065

To rename a post type labels, we need to manipulate the global $wp_post_types. Note that the post type is portfolio and that the labels are just dummy text.

add_action( 'init', 'object_label_so_16097216', 0 );

function object_label_so_16097216()
{
    global $wp_post_types;
    $labels = &$wp_post_types['portfolio']->labels; // <-- adjust CPT

    $labels->name = 'name'; // <-- adjust from here on
    $labels->singular_name = 'singular_name';
    $labels->add_new      = 'add_new';
    $labels->add_new_item = 'add_new';
    $labels->edit_item = 'edit_item';
    $labels->new_item = 'name';
    $labels->view_item = 'view_item';
    $labels->search_items = 'search_items';
    $labels->not_found = 'not_found';
    $labels->not_found_in_trash = 'not_found_in_trash';
}

After that, we still need to change the menu item labels. And here, I'm using an auxiliary function to get the exact position of the CPT menu item. And the labels are dummy text again.

add_action( 'admin_menu', 'menu_label_so_16097216' , 0 );

function menu_label_so_16097216()
{
    global $menu, $submenu;

    $cpt_link = 'edit.php?post_type=portfolio'; // <-- adjust CPT
    $position = b5f_recursive_array_search( $cpt_link, $menu );

    $menu[$position][0] = 'name'; // <-- adjust from here on
    $submenu[$cpt_link][5][0] = 'name';
    $submenu[$cpt_link][10][0] = 'add_new';       
}

function b5f_recursive_array_search( $needle, $haystack ) 
{
    foreach( $haystack as $key => $value ) 
    {
        $current_key = $key;
        if( 
            $needle === $value 
            OR ( 
                is_array( $value )
                && b5f_recursive_array_search( $needle, $value ) !== false 
            )
        ) 
        {
            return $current_key;
        }
    }
    return false;
}

Just drop ALL this code in the child's functions.php, adjust CPT and labels, and good to go.

Based in this WordPress Answer.

Upvotes: 2

Related Questions