0

I'm developping a small plugin to rename existing custom post type from a theme (Divi). I use this code :

add_filter( 'register_post_type_args', 'rename_project');
function rename_project( $args, $post_type ) {
    // Let's make sure that we're customizing the post type we really need
    if ( 'project' === $post_type ) {

        $singular = get_option('ncd_title_field');
        $plural = $singular.'s';
        $title = ucfirst($plural);
        
        $args['labels']['name'] = $title;
        $args['labels']['singular_name'] = $singular;
        $args['labels']['menu_name'] = $title;
        $args['labels']['name_admin_bar'] = $title;
        $args['labels']['add_new_item'] = __('Add ','nexus-collaborators-divi') . $singular;
        $args['labels']['add_new'] =  __('Add new ','nexus-collaborators-divi');
        $args['labels']['new_item'] =  __('Add ','nexus-collaborators-divi');
        $args['labels']['edit_item'] =  __('Edit ','nexus-collaborators-divi') . $singular;
        $args['labels']['view_item'] =  __('View ','nexus-collaborators-divi') . $singular;
        $args['labels']['all_items'] = __('View ','nexus-collaborators-divi') . $plural;
        $args['labels']['search_items'] = __('Search ','nexus-collaborators-divi') . $plural;
        $args['labels']['archives'] = __('Archives ','nexus-collaborators-divi') . $plural;
        $args['labels']['attributes'] = __('Attributes ','nexus-collaborators-divi') . $plural;
        $args['labels']['update_item'] = __('Updates ','nexus-collaborators-divi');
        $args['labels']['view_items'] = __('View all ','nexus-collaborators-divi') . $plural;
        $args['labels']['not_found'] = __('Not found ','nexus-collaborators-divi');
        $args['labels']['featured_image'] = __('Featured image ','nexus-collaborators-divi');
        $args['labels']['set_featured_image'] = __('Set featured image ','nexus-collaborators-divi');
        $args['labels']['remove_featured_image'] = __('Remove featured image ','nexus-collaborators-divi');
        $args['labels']['use_featured_image'] = __('Use featured image ','nexus-collaborators-divi');
        $args['labels']['insert_into_item'] = __('Insert','nexus-collaborators-divi');
        $args['labels']['uploaded_to_this_item'] = __('Uploaded to this item','nexus-collaborators-divi');
        $args['labels']['items_list'] = __('List ','nexus-collaborators-divi ') . $plural;
        $args['labels']['items_list_navigation'] = __('Navigation list ','nexus-collaborators-divi') . $plural;
        $args['labels']['filter_items_list'] = __('Filter list ','nexus-collaborators-divi') . $plural;
        $args['rewrite']['slug'] = $plural;
        $args['menu_icon'] = get_option('ncd_ico_field','dashicons-admin-post');
        $args['menu_position'] = 5;
        
        // Flushing the rewrite rules should make sure that you don't have to re-save your permalink structure
        flush_rewrite_rules();
    }//end if
    
    // Finally return or altered labels
    return $args;
}

add_filter('register_taxonomy_args', 'dp_register_taxonomy_args');
function dp_register_taxonomy_args($args, $taxonomy, $object_type){
    
    // If it's either the project_category taxonomy, we apply the new name
    if ('project_category' == $taxonomy && !empty(get_option('ncd_category_field')) ) {

        $singular = get_option('ncd_category_field');
        $plural = $singular.'s';
        $title = ucfirst($plural);

        $args['labels']['name'] = $title;
        $args['labels']['singular_name'] =$singular;
        $args['labels']['menu_name'] =$singular;
        $args['labels']['all_items'] =__('All','nexus-collaborators-divi') . $plural;
        $args['labels']['new_item_name'] =__('New ','nexus-collaborators-divi') . $singular;
        $args['labels']['add_new_item'] =__('Add ','nexus-collaborators-divi') . $singular;
        $args['labels']['edit_item'] =__('View ','nexus-collaborators-divi') . $singular;
        $args['labels']['update_item'] =__('Update ','nexus-collaborators-divi');
        $args['labels']['view_item'] =__('View ','nexus-collaborators-divi');
        
    }

    // Or if it's the project_tag taxonomy, we apply the new name
    if ('project_tag' == $taxonomy && !empty(get_option('ncd_tag_field'))) {
        $singular = get_option('ncd_tag_field');
        $plural = $singular.'s';
        $title = ucfirst($plural);

        $args['labels']['name'] = $title;
        $args['labels']['singular_name'] =$singular;
        $args['labels']['menu_name'] =$singular;
        $args['labels']['all_items'] =__('All','nexus-collaborators-divi') . $plural;
        $args['labels']['new_item_name'] =__('New ','nexus-collaborators-divi') . $singular;
        $args['labels']['add_new_item'] =__('Add ','nexus-collaborators-divi') . $singular;
        $args['labels']['edit_item'] =__('View ','nexus-collaborators-divi') . $singular;
        $args['labels']['update_item'] =__('Update ','nexus-collaborators-divi');
        $args['labels']['view_item'] =__('View ','nexus-collaborators-divi');
    }

    // Finally return the taxonomy_args
    return $args;
}

I use an option page to save and retrieve the new name.

I also translated my plugin to French but I have a problem : The labels of the cpt and taxnonomy renamed with my new textdomain are not translated but the translation is in the po file. Everything else works fine, except for these ones

I think it's because these labels are already configured with another textdomain.

Do you know how to correctly translate these labels ?

1
  • I suspect this isn't possible. The functionality responsible for translations only understands text domains that are strings when the post type or taxonomy is registered. It has no concept of variables or filters.
    – vancoder
    Aug 5 at 20:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.