Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Comments: Introduce a register_comment_type() API#12311

Open
adamsilverstein wants to merge 4 commits into
WordPress:trunkfrom
adamsilverstein:feature/register-comment-type
Open

Comments: Introduce a register_comment_type() API#12311
adamsilverstein wants to merge 4 commits into
WordPress:trunkfrom
adamsilverstein:feature/register-comment-type

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Jun 24, 2026

Copy link
Copy Markdown
Member

Description

Trac #35214 is the long-running tracking ticket for custom comment types. Its repeated consensus (dshanske, jeremyfelt) has been "backend first, smaller steps." The data-storage groundwork already shipped in 5.5 via #49236 (comments now store comment_type = 'comment' instead of ''). This PR adds the next logical slice: a comment type registration API modeled on the post type and taxonomy APIs.

What this adds

  • WP_Comment_Type class (src/wp-includes/class-wp-comment-type.php), a trimmed mirror of WP_Taxonomy.
  • register_comment_type(), unregister_comment_type(), get_comment_type_object(), get_comment_types(), and comment_type_exists().
  • create_initial_comment_types() registering the built-in comment, pingback, trackback, and note types, hooked on init (priority 0) and change_locale. The note type (added in 6.9 for editor Notes) is marked internal.
  • get_comment_type_labels(), reusing the shared _get_custom_object_labels() helper.
  • The comment_type() template tag now falls back to a registered, non-built-in type's singular label when the caller supplies no custom text. Built-in output and explicit overrides are byte-for-byte unchanged.

Filters mirror the post type/taxonomy conventions: register_comment_type_args, register_{$type}_comment_type_args, comment_type_labels_{$type}, and the registered_comment_type / registered_comment_type_{$type} / unregistered_comment_type actions.

Scope / non-goals

This is deliberately a small, non-breaking step. Registration provides labels and metadata only; it does not constrain values stored in the comment_type column and makes no change to WP_Comment_Query.

The internal flag is advisory metadata in this PR. Generalizing the hard-coded note exclusion in WP_Comment_Query is handled separately by #12310 (default_excluded_comment_types filter); these two PRs are complementary and do not overlap. Admin list-table/dropdown integration and capabilities are left to follow-ups (the admin UI is design-feedback gated per discussion on #35214).

Testing

New tests under tests/phpunit/tests/comment/:

  • types.php — registration, unregistration (including blocking built-ins), get_comment_types() filtering, actions, and label filters.
  • wpCommentType.phpWP_Comment_Type defaults, arg filters, and default-label caching.
  • commentType.phpcomment_type() output for built-in types (unchanged), explicit overrides, and registered custom type labels.

All 580 --group comment tests pass; post/types.php still passes (shared label helper unaffected); PHPCS is clean on changed files.

See #35214

Add a comment type registration API modeled on the post type and taxonomy
APIs, a first step toward custom comment types. Introduce the WP_Comment_Type
class and register_comment_type(), unregister_comment_type(),
get_comment_type_object(), get_comment_types(), and comment_type_exists().

Register the built-in comment, pingback, trackback, and note types via
create_initial_comment_types(), hooked on init at priority 0 and on
change_locale so labels re-translate. The note type is marked internal.

Make the comment_type() template tag fall back to a registered type's
singular label for non-built-in types when no custom text is supplied.
Built-in output and explicit overrides are unchanged.

Registration provides labels and metadata only; it does not constrain the
values stored in the comment_type column or alter WP_Comment_Query behavior.

See #35214.
Cover register_comment_type(), unregister_comment_type(),
get_comment_type_object(), get_comment_types(), comment_type_exists(), the
WP_Comment_Type class, and the registered/unregistered actions and label
filters. Verify the built-in types are registered and cannot be unregistered,
and that the comment_type() template tag renders registered type labels while
preserving built-in output.

See #35214.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…abel.

Omit 'label' from the WP_Comment_Type::set_props() defaults so the property
stays null when not provided. A false default was treated as a supplied value
by _get_custom_object_labels(), which overwrote the default name with false,
leaving labels->name and label as false for any type registered without an
explicit label. This mirrors WP_Post_Type and WP_Taxonomy, which omit 'label'
from their defaults for the same reason.

Add a regression test covering default labels for a label-less comment type.

See #35214.
The default branch of comment_type() echoed a registered type's
singular_name label without escaping. Labels are developer-supplied
(via register_comment_type() or the comment_type_labels_{$type}
filter) and reach the public comment list unescaped, so wrap the
output in esc_html() to match the post type and taxonomy label
contract. Document that labels are stored unescaped and add a
regression test asserting an HTML payload in the label is escaped.
adamsilverstein added a commit to adamsilverstein/wordpress-develop that referenced this pull request Jun 25, 2026
Expose registered comment types through a read-only `/wp/v2/comment-types`
controller, mirroring the post types controller (`/wp/v2/types`). This lets
REST clients discover the registered types and their labels, which the block
editor's inline-commenting work needs in order to add and query comments by
type.

Add a `show_in_rest` argument to `WP_Comment_Type` (defaulting to the value of
`public`, as `show_ui` does) to gate which types the endpoint exposes. The
built-in `comment`, `pingback`, and `trackback` types are public and therefore
visible; the internal `note` type is not.

Builds on the registration API in WordPress#12311.

See #35214.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant