This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
As a fellow WordPress developer, you know the importance of hooks in customizing your WordPress site without altering the core files. Today, let’s dive into the display_post_states filter. It filters the default post display states used in the posts list table. Using this hook, you can customize how post states are displayed in the admin panel.
Example 1: Always Display Current Post Status
This example demonstrates how you can always show the current post status in the labels.
/** * This always shows the current post status in the labels. * * @param array $states current states. * @param WP_Post $post current post object. * @return array */ function weplugins_custom_display_post_states( $states, $post ) { /* Receive the post status object by post status name */ $post_status_object = get_post_status_object( $post->post_status ); }
Example 2: Modify Post States Based on Conditions
In this example, learn how to modify the $post_states variable according to your website requirements.
function weplugins_modify_display_post_states_defaults($post_states, $post) { // Update the $post_states variable according to your website requirements and return this variable. You can modify the $post_states variable conditionally too if you want. return $post_states; } // add the filter add_filter( "display_post_states", "weplugins_modify_display_post_states_defaults", 10, 2 );
Example 3: Removing a Hook Callback
Here’s how you can remove a registered hook using remove_filter.
remove_filter( "display_post_states", "weplugins_modify_display_post_states_defaults", 10, 2 );
Please ensure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
If you have any trouble using this hook or need further customization, please contact us at WePlugins and we’d be happy to assist you.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.