WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#20005 closed enhancement (invalid)

Use single and plural Label arguments to populate defaults for Labels in register_post_type

Reported by: ericlewis Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: close
Focuses: Cc:

Description

When creating a post type, the $labels array seems a bit superfluous to me, given that there is a singular name for the post type and an option for a plural one in the argument 'label'. Including every custom label string as arguments also tends to bloat code for each post type definition, and I've begun to use a wrapper for register_post_types to deal with this.

I support using the arguments $labelsname? and $labelssingular_name? to fill the rest of the default labels for custom post types.

Change History (5)

comment:1 duck_2 years ago

  • Keywords close added

I believe you mean doing something like:

$labels = array(
  // ...
  'view_item' => 'View ' . $labels['singular_name'],
  // ...
);

While this works in English it does not work when translating the strings into another language, see #12968.

comment:2 nacin2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

comment:3 ericlewis2 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Sorry if I'm being daft or unclear, but I'm referring to the use of "post" and "page" throughout the get_post_type_labels() function in post.php:1182.

I'm wondering about the utility of the defaults here, other than to fill defaults for the "post" and "page" post types. I'm suggesting the arguments for the internationalization strings could be auto-populated. Wouldn't this still allow for internationalization of each label string?

function get_post_type_labels( $post_type_object ) {
	$nohier_singular = $post_type_object->labels['singular_name'] ? $post_type_object->labels['singular_name'] : "Post";
	$nohier_plural = $post_type_object->labels['name'] ? $post_type_object->labels['name'] : "Posts";
	$hier_singular = $post_type_object->labels['singular_name'] ? $post_type_object->labels['singular_name'] : "Page";
	$hier_plural = $post_type_object->labels['name'] ? $post_type_object->labels['name'] : "Pages";

	$nohier_vs_hier_defaults = array(
		'name' => array( _x( $nohier_plural, 'post type general name'), _x($hier_plural, 'post type general name') ),
		'singular_name' => array( _x( $nohier_singular, 'post type singular name'), _x($hier_singular, 'post type singular name') ),
		'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
		'add_new_item' => array( __('Add New ' . $nohier_singular ), __('Add New ' . $hier_singular) ),
		'edit_item' => array( __('Edit ' . $nohier_singular), __('Edit ' . $hier_singular ) ),
		'new_item' => array( __('New ' . $nohier_singular), __('New ' . $hier_singular ) ),
		'view_item' => array( __('View ' . $nohier_singular), __('View ' . $hier_singular ) ),
		'search_items' => array( __('Search ' . $nohier_plural ), __('Search ' . $hier_plural ) ),
		'not_found' => array( __('No ' . strtolower($nohier_plural) . ' found.'), __('No ' . strtolower($hier_plural) . ' found.') ),
		'not_found_in_trash' => array( __('No ' . strtolower($nohier_plural) . ' found in Trash.'), __('No ' . strtolower($hier_plural) . ' found in Trash.') ),
		'parent_item_colon' => array( null, __('Parent ' . $hier_singular . ':') ),
		'all_items' => array( __( 'All ' . $nohier_plural ), __( 'All ' . $hier_plural ) )
	);
	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
	return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
}

So that registering a new post type would require a lot less code to get good labels throughout the admin section:

register_post_type(
	"bug-report", 
	array( "labels" => 
		array( 
			"name" => "Bug Reports",
			"plural" => "Bug Report"
		)
	)
);

Sorry if I'm missing something.

comment:4 dd322 years ago

Sorry if I'm missing something.

Basically, Given the string "Add new %s", It'll be translated differently for Posts, Pages, and other types.

For example, in my imaginary language, That string would translate to "Goobly Duck Post" and "Gably Frog Page" (although, seriously, some languages do differ in the translation depending on the context), leading to a single string with substitution being unusable for purposes of translation.

Of course, this isn't much of a problem for most CPT's, as they'll not be translated, or if they are, they'd have the full labels array filled in..

comment:5 ericlewis2 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Ah ok, thanks for the clear up.

Note: See TracTickets for help on using tickets.