2

I have a WordPress parent theme that uses a custom post type called portfolio, which I'd like to change 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 answer, but 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.

3

$wp_post_types is a global array that holds post_type objects, which in turn have a labels property.
You can change $wp_post_types[$post_type]->labels after the parent theme has set the CPT.

Add higher priority to init hook.

Add the following code in your theme's functions.php file.

For more information, check out the codex article on get_post_type_object.

function change_post_object_label() {
    global $wp_post_types;
    $labels = &$wp_post_types['portfolio']->labels;
    $labels->name = 'Property';
    $labels->singular_name = 'Property';
    $labels->add_new = 'Add Property';
    $labels->add_new_item = 'Add Property';
    $labels->edit_item = 'Edit Property';
    $labels->new_item = 'Property';
    $labels->all_items = 'All Properties';
    $labels->view_item = 'View Property';
    $labels->search_items = 'Search Property';
    $labels->not_found = 'No Property found';
    $labels->not_found_in_trash = 'No Property found in Trash';    
}
add_action( 'init', 'change_post_object_label', 999 );

Add following code in functions.php to replace main menu label from sidebar

function change_post_menu_label() {
  global $menu;
  //print_r($menu); Print menus and find out the index of your custom post type menu from it.
  $menu[27][0] = 'Bacons'; // Replace the 27 with your custom post type menu index from displayed above $menu array 
}
add_action( 'admin_menu', 'change_post_menu_label' );

Add following code in functions.php for adding image to admin sidebar CPT menu

add_action('admin_head', 'change_CPT_icon');
function change_CPT_icon() {?>
  <style>
    #menu-posts-portfolio .wp-menu-image { background: url('<?php echo  get_bloginfo('template_directory').'/includes/images/new-image.png';?>') no-repeat 5px 5px transparent !important; }     
  </style>
  <?php
}
5
  • Well, it seems to work, with one exception. The main label on the sidebar is not changing. It looks like "name" should be changing that. Any ideas? – Jeremy Miller Apr 19 '13 at 4:40
  • Vinod, I'm curious if it's this simple to change the arguments for the custom post type. I'm trying to change the image used in the menu. I've got: $args = &$wp_post_types['portfolio']->args; $args->menu_icon = get_template_directory_uri() .'/includes/images/new-image.png'; but this doesn't seem to work. – Jeremy Miller Apr 19 '13 at 5:16
  • @JeremyMiller I have added the code in answer to change the image used in the menu. – Vinod Dalvi Apr 19 '13 at 6:12
  • Hi Vinod, I'm not sure that will work. That's changing the background of the div, where the current function adds the actual image. So this would change the background, but the original image would still show up. Thoughts? – Jeremy Miller Apr 19 '13 at 16:03
  • @JeremyMiller It will overwrite the original image. – Vinod Dalvi Apr 20 '13 at 9:00
0

This changes the labels and the menu name. Also a bonus to change the slug too.

This example changes a registered post type called 'services' to 'specialty' (American English spelling).

Change the labels and the name all at once:

add_action( 'init', 'yourprefix_change_cpt_service_to_specialty_labels' );
/**
 * Change Post Type Post Labels to Blog
 * Reference(s):
 * https://wpbeaches.com/change-the-wordpress-post-type-name-to-something-else/
 * https://revelationconcept.com/wordpress-rename-default-posts-news-something-else/
 */
function yourprefix_change_cpt_service_to_specialty_labels() {

    if ( post_type_exists( 'services' ) ) :

        $get_post_type = get_post_type_object('services');
        $labels = $get_post_type->labels;
        $labels->name               = 'Specialty';
        $labels->singular_name      = 'Specialty Page';
        $labels->add_new            = 'Add Specialty Page';
        $labels->add_new_item       = 'Add Specialty Page';
        $labels->edit_item          = 'Edit Specialty Page';
        $labels->new_item           = 'New Specialty Page';
        $labels->view_item          = 'View Specialty Page';
        $labels->search_items       = 'Search Specialty Pages';
        $labels->not_found          = 'No Specialties found';
        $labels->not_found_in_trash = 'No Specialty Pages found in Trash';
        $labels->all_items          = 'All Specialty Pages';
        $labels->menu_name          = 'Specialties';
        $labels->name_admin_bar     = 'Specialty Page';

    endif;
}

Bonus: Change the slug. Don't forget to save your permalinks after this is added.

add_filter( 'register_post_type_args', 'yourprefix_change_post_types_slug_service', 10, 2 );
// Change Slug of Registered Post Type for Services // Resave your PERMALINKS or this will return 404!!!
function yourprefix_change_post_types_slug_service( $args, $post_type ) {

   /*item post type slug*/   
   if ( 'services' === $post_type ) :
      $args['rewrite']['slug'] = 'specialty';
   endif;

   return $args;
}

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.