WordPress i18n-friendly preg_replace post title

We can use the protected_title_format and private_title_format filters, available since WordPress version 2.8:

 add_filter( 'protected_title_format', 'wpse_pure_title', 10, 2 );
 add_filter( 'private_title_format',   'wpse_pure_title', 10, 2 );

 function wpse_pure_title( $format, \WP_Post $post )
 {
    return  'mycpt' === get_post_type( $post ) ? '%s' : $format;
 }

to get rid of the “Private:” and “Protected:” parts on the front-end for the mycpt post type.

We don’t need a is_admin() check here, because these filters only run in the front-end.

Leave a Comment