Last updated on September 14, 2023

Change the WordPress Post Type Labels

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Modify post type labels for your site.

Changing the WordPress post type labels can be useful when you want to customize the labels that appear in the WordPress admin area for a specific post type. This can help make your website more user-friendly and align the labels with the specific content you are working with.

To change the post type labels, you can use the register_post_type() function and pass an array of arguments that includes the labels parameter. Within the labels parameter, you can specify the desired labels for the post type.

Here’s an example code snippet that demonstrates how to change the labels for a custom post type called “books”:

function wpsnippets_change_book_labels() {
    $labels = array(
        'name'                  => _x( 'Books', 'Post type general name', 'textdomain' ),
        'singular_name'         => _x( 'Book', 'Post type singular name', 'textdomain' ),
        'menu_name'             => _x( 'Books', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'        => _x( 'Book', 'Add New on Toolbar', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New Book', 'textdomain' ),
        'new_item'              => __( 'New Book', 'textdomain' ),
        'edit_item'             => __( 'Edit Book', 'textdomain' ),
        'view_item'             => __( 'View Book', 'textdomain' ),
        'all_items'             => __( 'All Books', 'textdomain' ),
        'search_items'          => __( 'Search Books', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent Books:', 'textdomain' ),
        'not_found'             => __( 'No books found.', 'textdomain' ),
        'not_found_in_trash'    => __( 'No books found in Trash.', 'textdomain' ),
        'featured_image'        => _x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'              => _x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'      => _x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'     => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'            => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
    );

    $args = array(
        'labels' => $labels,
        // Other arguments for the custom post type...
    );

    register_post_type( 'books', $args );
}
add_action( 'init', 'wpsnippets_change_book_labels' );

In this example, we define a function wpsnippets_change_book_labels() that is hooked into the init action. Inside the function, we define an array of labels for the “books” post type. We then pass this array as the value for the labels parameter when registering the post type using the register_post_type() function.

Note that you should replace 'textdomain' with the appropriate text domain for your theme or plugin.

By using this code snippet and modifying the labels to fit your needs, you can easily change the WordPress post type labels for a custom post type.

Examples

Example 1: Change the labels for the default “Post” post type

This example demonstrates how to change the labels for the default “Post” post type in WordPress. By modifying the labels, you can customize the text displayed in the WordPress admin area for this post type.

function wpsnippets_change_post_type_labels( $labels ) {
    $labels->name = 'Articles';
    $labels->singular_name = 'Article';
    $labels->add_new = 'Add New Article';
    $labels->add_new_item = 'Add New Article';
    $labels->edit_item = 'Edit Article';
    $labels->new_item = 'New Article';
    $labels->view_item = 'View Article';
    $labels->search_items = 'Search Articles';
    $labels->not_found = 'No articles found';
    $labels->not_found_in_trash = 'No articles found in Trash';
    $labels->all_items = 'All Articles';
    $labels->menu_name = 'Articles';
    $labels->name_admin_bar = 'Article';
    return $labels;
}
add_filter( 'post_type_labels_post', 'wpsnippets_change_post_type_labels' );

In this code example, we define a custom function wpsnippets_change_post_type_labels that accepts the $labels object as a parameter. We then modify the desired labels using the object’s properties. Finally, we use the add_filter function to apply our custom labels to the “Post” post type.

Example 2: Change the labels for a custom post type

This example demonstrates how to change the labels for a custom post type in WordPress. By modifying the labels, you can customize the text displayed in the WordPress admin area for your custom post type.

function wpsnippets_change_custom_post_type_labels( $labels ) {
    $labels->name = 'Products';
    $labels->singular_name = 'Product';
    $labels->add_new = 'Add New Product';
    $labels->add_new_item = 'Add New Product';
    $labels->edit_item = 'Edit Product';
    $labels->new_item = 'New Product';
    $labels->view_item = 'View Product';
    $labels->search_items = 'Search Products';
    $labels->not_found = 'No products found';
    $labels->not_found_in_trash = 'No products found in Trash';
    $labels->all_items = 'All Products';
    $labels->menu_name = 'Products';
    $labels->name_admin_bar = 'Product';
    return $labels;
}
add_filter( 'post_type_labels_custom_post_type', 'wpsnippets_change_custom_post_type_labels' );

In this code example, we define a custom function wpsnippets_change_custom_post_type_labels that accepts the $labels object as a parameter. We then modify the desired labels using the object’s properties. Finally, we use the add_filter function to apply our custom labels to the specified custom post type.

Example 3: Change the labels for multiple post types

This example demonstrates how to change the labels for multiple post types in WordPress. By modifying the labels, you can customize the text displayed in the WordPress admin area for each post type individually.

function wpsnippets_change_multiple_post_type_labels( $labels ) {
    $labels->name = 'Events';
    $labels->singular_name = 'Event';
    $labels->add_new = 'Add New Event';
    $labels->add_new_item = 'Add New Event';
    $labels->edit_item = 'Edit Event';
    $labels->new_item = 'New Event';
    $labels->view_item = 'View Event';
    $labels->search_items = 'Search Events';
    $labels->not_found = 'No events found';
    $labels->not_found_in_trash = 'No events found in Trash';
    $labels->all_items = 'All Events';
    $labels->menu_name = 'Events';
    $labels->name_admin_bar = 'Event';
    return $labels;
}
add_filter( 'post_type_labels_event', 'wpsnippets_change_multiple_post_type_labels' );
add_filter( 'post_type_labels_another_post_type', 'wpsnippets_change_multiple_post_type_labels' );

In this code example, we define a custom function wpsnippets_change_multiple_post_type_labels that accepts the $labels object as a parameter. We then modify the desired labels using the object’s properties. Finally, we use the add_filter function to apply our custom labels to multiple post types by specifying each post type individually.

Last updated on September 14, 2023. Originally posted on September 18, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *