custom post types – Hide Status Option From WordPress Publish Metabox and Rename Published on:

Question

Both those strings seem to be located in wp-admin/includes/meta-boxes.php. They don’t have any filters attached to them, so they’re not meant to be (easily) altered by your code.

However, both those strings are internationalized (ie, run through a translation function) to allow WordPress’s interface to be translated into languages other than English. The i18n functions—__() and _e()—both use translate(), which provides a filter, gettext, that you can use to control what is displayed.

Caveats:

  • This may adversely affect the translatability of your plugin.
  • If WordPress ever changes the strings used in these locations, your filter will no longer work.

With that in mind, this code should be a good starting point.

add_filter( 'gettext', 'wpse406946_change_meta_strings', 10, 3 );

/**
 * Change the "Published On:" string and remove the "status: " line from 
 * my CPT's meta box.
 *
 * @param  string $translation The translated string.
 * @param  string $text        The original string.
 * @param  string $domain      The current text domain.
 * @return string              The filtered translated text.
 */
function wpse406946_change_meta_strings( $translation, $text, $domain ) {
    // Checks for the custom post type.
    if ( 'my_post_type' === get_post_type() && 'default' === $domain ) {
        // Unhooks the filter to prevent infinite loops.
        remove_filter( 'gettext', 'wpse406946_change_meta_strings', 10, 3 );
        if ( 'Published on: %s' === $text ) {
            $translation = __( 'Created on: $%s', $domain );
        }
        // The "Status: Published" message is actually 2 parts.
        // This should catch both.
        if ( 'Status:' === $text || 'Published' === $text ) {
            $translation = '';
        }

        // Rehooks the filter.
        add_filter( 'gettext', 'wpse406946_change_meta_strings', 10, 3 );
    }

    return $translation;
}

References

0
jamesnelson82 5 months 2022-06-20T12:05:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse