WordPress.org

Forums

Whistles
[resolved] Change Post Name Labels (2 posts)

  1. Mark Wilkinson
    Member
    Posted 1 year ago #

    I am looking to rename the post type labels set when using register_post_type to a different name - from Whistle(s) to Snippet(s). This can be done it seems with the following filter:

    post_type_labels_whistle

    I gave this a go with the following code in my theme functions.php file:

    function pxlcore_whistles_post_labels( $labels ) {
    
    	$labels = array(
    		'name'               => __( 'Snippets',                   'whistles' ),
    		'singular_name'      => __( 'Snippet',                    'whistles' ),
    		'menu_name'          => __( 'Snippets',                   'whistles' ),
    		'name_admin_bar'     => __( 'Snippet',                    'whistles' ),
    		'add_new'            => __( 'Add New',                    'whistles' ),
    		'add_new_item'       => __( 'Add New Snippet',            'whistles' ),
    		'edit_item'          => __( 'Edit Snippet',               'whistles' ),
    		'new_item'           => __( 'New Snippet',                'whistles' ),
    		'view_item'          => __( 'View Snippet',               'whistles' ),
    		'search_items'       => __( 'Search Snippets',            'whistles' ),
    		'not_found'          => __( 'No snippets found',          'whistles' ),
    		'not_found_in_trash' => __( 'No snippets found in trash', 'whistles' ),
    		'all_items'          => __( 'Snippets',                   'whistles' ),
    	);
    
    	return $labels;
    }
    
    add_filter( 'post_type_labels_whistle', 'pxlcore_whistles_post_labels' );

    All that happened was all the labels disappeared. On further investigation is seems that the $labels passed to the filter are an object class not an array which is what I was pushing back to the filter. I suspect this is why it fails. The object class passed is:

    object(stdClass)#107 (14) {
      ["name"]=>"Whistles"
      ["singular_name"]=>"Whistle"
      ["add_new"]=>"Add New"
      ["add_new_item"]=>"Add New Whistle"
      ["edit_item"]=>"Edit Whistle"
      ["new_item"]=>"New Whistle"
      ["view_item"]=>"View Whistle"
      ["search_items"]=>"Search Whistles"
      ["not_found"]=>"No whistles found"
      ["not_found_in_trash"]=>"No whistles found in trash"
      ["parent_item_colon"]=>NULL
      ["all_items"]=>"Whistles"
      ["menu_name"]=>"Whistles"
      ["name_admin_bar"]=>"Whistle"
    }

    Therefore my question if anyone can help out, is how can I manipulate that object class in my function and pass the new one with the new labels back to the filter? Many thanks in advance.

    https://wordpress.org/plugins/whistles/

  2. Mark Wilkinson
    Member
    Posted 1 year ago #

    Oops I was being very dumb. Below is how to change the post labels for the Whistle post type:

    /***************************************************************
    * Function pxlcore_whistles_post_labels()
    * Changes the post labels for the Whistles post type.
    ***************************************************************/
    function pxlcore_whistles_post_labels( $labels ) {
    
    	$labels->name = 'Snippets';
    	$labels->singular_name = 'Snippet';
    	$labels->menu_name = 'Snippets';
    	$labels->admin_bar_name = 'Snippet';
    	$labels->add_new = 'Add New';
    	$labels->add_new_item = 'Add New Snippet';
    	$labels->edit_item = 'Edit Snippet';
    	$labels->new_item = 'New Snippet';
    	$labels->view_item = 'View Snippet';
    	$labels->search_items = 'Search Snippets';
    	$labels->not_found = 'No snippets found';
    	$labels->not_found_in_trash = 'No snippets found in trash';
    	$labels->all_items = 'Snippets';
    
    	return $labels;
    
    }
    
    add_filter( 'post_type_labels_whistle', 'pxlcore_whistles_post_labels' );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic