Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensions to customize the template name in the 'Add template' screen #60283

Closed
Aljullu opened this issue Mar 28, 2024 · 4 comments · Fixed by #60367
Closed

Allow extensions to customize the template name in the 'Add template' screen #60283

Aljullu opened this issue Mar 28, 2024 · 4 comments · Fixed by #60367
Assignees
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement. [Type] New API New API to be used by plugin developers or package users.

Comments

@Aljullu
Copy link
Contributor

Aljullu commented Mar 28, 2024

Description

When creating a new template, Custom Post Type taxonomies are displayed with not very user-friendly titles. Ie: Recipe (recipe_cat).

That's not ideal because:

  1. Users depend on slugs to distinguish between templates.
  2. It's not very obvious that Category (recipe_cat) means the Recipes by Category template.

Add template screen

Currently, the values which are used are the singular_name label and the taxonomy slug:

menuItemTitle = sprintf(
// translators: %1s: Name of the taxonomy e.g: "Category"; %2s: Slug of the taxonomy e.g: "product_cat".
__( '%1$s (%2$s)' ),
labels.singular_name,
slug
);

However, that doesn't give enough flexibility to 3rd-party plugins because singular_name is used in other parts of the UI so it can't be adapted to the context of the Site Editor.

A real-life example

While the Recipe example I showed above is quite simple, there are other plugins which might add more templates, like WooCommerce:

Add template screen with WooCommerce

(Sidenote: currently WooCommerce doesn't show these templates in the 'Add template' screen, but we would like to: woocommerce/woocommerce#42532)

Suggested solutions

I would like to propose some possible solutions that, I believe, would help improve this part of the UI:

  1. Adding a new label value in the register_taxonomy() WordPress function intended to be used to name the templates in the admin. In the Product Category case, that could be Product Category Archives. If a plugin registers a taxonomy without specifying that value, the singular_name could be used as a fallback.
  2. If, for some reason, we want to avoid the first approach, we could add a JS filter to let plugins customize the elements in the 'Add template' screen. That means using a less-explicit filter hook instead of the most explicit API from approach 1; however, it would have the advantage that it could be made so 3rd-plugins can also customize the template icon, for example.
  3. Modifying the UI of the Add template screen to keep CPT templates separate from the others. If we could add a header for each specific CPT or plugin that registers custom templates, using singular_name would automatically become more use friendly. In the WC example, I'm thinking that there could be a Product or WooCommerce header where all product-related templates could be grouped. Ideally that would require no work by plugins.

Step-by-step reproduction instructions

  1. Add this code snippet to your site in order to create a Recipes post type and a Recipe category taxonomy (you can use Code Snippets plugin if needed):
add_action( 'init', function () {
	register_post_type(
		'recipe',
		array(
			'labels'       => array(
				'name' => 'Recipes',
			),
			'show_in_rest' => true,
		)
	);
	register_taxonomy(
		'recipe_cat',
		'recipe',
		array(
			'label'        => 'Categories',
			'labels'       => array(
				'name'          => 'Recipe categories',
				'singular_name' => 'Category',
			),
			'show_in_rest' => true,
		)
	);
} );
  1. Go to Appearance > Editor > Templates > Add New Template.
  2. Once the 'Add template' screen shows up, notice how the title of the custom taxonomy template is not very user friendly: Category (recipe_cat).

Expected result:

As a user, I don't need to know that Recipe Categories uses the recipe_cat slug internally. Ideally, those buttons should show up a user-friendly name like: Recipe Category Archives or Recipes by Category.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Aljullu Aljullu added [Type] New API New API to be used by plugin developers or package users. [Feature] Template Editing Mode Related to the template editor available in the Block Editor labels Mar 28, 2024
@Aljullu
Copy link
Contributor Author

Aljullu commented Mar 28, 2024

cc @gziolo and @ntsekouras to know your thoughts about the above. I think the questions are:

  1. Do you agree this is an area where the UI could be improved, or would you prefer to keep it as-is?
  2. If you agree that we could introduce changes, do any of the proposed solutions I wrote sound like a good approach to you?

I would be more than happy to contribute fixing this, but would like to hear your thoughts about it before getting started. 🙂

@ntsekouras
Copy link
Contributor

Thanks for the issue @Aljullu! For me your first suggestion would be the best:

Adding a new label value in the register_taxonomy() WordPress function intended to be used to name the templates in the admin. In the Product Category case, that could be Product Category Archives. If a plugin registers a taxonomy without specifying that value, the singular_name could be used as a fallback.

A similar label could be considered for post type's labels too.

Regarding grouping or reordering is a different issue and could considered as well, besides the label.

@annezazu annezazu added [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement. labels Apr 1, 2024
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Apr 2, 2024
@Aljullu
Copy link
Contributor Author

Aljullu commented Apr 2, 2024

Thanks for the input, @ntsekouras!

A similar label could be considered for post type's labels too.

Agree, good point.

I opened two PRs, one in Gutenberg and the other one in WordPress, which I think handle all the work related to introducing a new label when registering a CPT and Taxonomy:

Hope you don't mind I added you as the reviewer. 🙂

@mcsf
Copy link
Contributor

mcsf commented Apr 2, 2024

I agree with adding a label to the taxonomy type, that would be my first move.

That said, it's worth exploring UI improvements. If nothing else, to distinguish custom types from standard. I wonder what information we could use from the registration process to reliably determine provenance or namespace (like "Woo", etc.), avoiding the cost of another extension of the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement. [Type] New API New API to be used by plugin developers or package users.
Projects
None yet
4 participants