[wp-trac] [WordPress Trac] #51403: Add filter for _post_states as it outputs raw HTML
WordPress Trac
noreply at wordpress.org
Mon Oct 20 19:17:50 UTC 2025
#51403: Add filter for _post_states as it outputs raw HTML
-------------------------------------------------+-------------------------
Reporter: brandbrilliance | Owner:
| westonruter
Type: enhancement | Status: closed
Priority: normal | Milestone: 6.9
Component: Posts, Post Types | Version:
Severity: normal | Resolution: fixed
Keywords: good-first-bug has-test-info has- | Focuses: template
patch has-unit-tests |
-------------------------------------------------+-------------------------
Comment (by dmsnell):
can follow-up after merge, but it would be really handy to have some
examples here so people know what to expect. I find the combination of
`_string` and `HTML string` confusing, plus I don’t know what to expect
for this or the following array. Which post states are these?
the filter name might also be confusing especially in relation to the
`display_post_states` filter.
{{{#!php
<?php
/**
* Filters the default post display states used in the posts list table.
*
* @since 2.8.0
* @since 3.6.0 Added the `$post` parameter.
* @since 5.5.0 Also applied in the Customizer context. If any admin
functions
* are used within the filter, their existence should be
checked
* with `function_exists()` before being used.
*
* @param string[] $post_states An array of post display states.
* @param WP_Post $post The current post object.
*/
return apply_filters( 'display_post_states', $post_states, $post );
}}}
Might this be something more like `post_states_combined_html`?
Alternatively, this prints a sequence of SPAN elements and text nodes.
Would we accomplish a similar result if we wrapped the HTML in a
containing element with an ID or class name that someone could target with
the HTML API if they wanted to modify the string as a whole?
It seems to me like it’d be more useful to filter the function which
generates this HTML rather than a filter which passes the already-
processed HTML.
E.g. this filter leaves the specifics about the spans and `—` for
the filtering function to remove and change. In another construction we
could pass the post statuses into the filter for combination.
{{{#!php
<?php
$html = apply_filters( 'post_states_to_html', null, $post_states, $post );
if ( ! isset( $html ) ) {
// Do what is already in `_post_states()`;
}
}}}
{{{#!php
<?php
add_filter( 'post_states_to_html', static function ( $html, $post_states,
$post ) {
if ( is_string( $html ) ) {
return $html;
}
$html = '<ul class="post-states-list">';
foreach ( $post_states as $post_state ) {
$html .= '<li class="post-state">' . esc_html( $post_state
) . '</li>';
}
$html .= '<li class="post-state">Sample State</li>';
$html .= '</ul>';
return $html;
} );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51403#comment:21>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list