How do I filter the excerpt metabox description in admin?

I want to change the default “Excerpts are optional hand-crafted summaries of your content that can be used in your theme. Learn more about manual excerpts.” help text below the Excerpt input area to something more meaningful for my Custom Post Type.

I’ve done something similar with Post Title, using a “translation” filter, but how would I do this with the post excerpt?

Read More

Here is my current code:

add_filter('gettext', 'custom_rewrites', 10, 4);
function custom_rewrites($translation, $text, $domain) {

    global $post;

    $translations = &get_translations_for_domain($domain);
    $translation_array = array();

    switch ($post->post_type) {
        case 'model':
            $translation_array = array(
                'Enter title here' => 'Enter model name here',
                'Excerpt' => "Byline",
                'Excerpts are optional hand-crafted summaries of your content that can be used in your theme.' => "Foobar"
            );
            break;
    }

    if (array_key_exists($text, $translation_array)) {
        return $translations->translate($translation_array[$text]);
    }

    return $translation;
}

The third translation isn’t working?

Related posts

Leave a Reply

3 comments

  1. This description is generated by post_excerpt_meta_box() function and is not passed through any explicit filters. It is however echoed by translation-related _e() function and so passes through gettext filter (which from your question you are already familiar with).

    As for limiting it to your CPT, I think current post type in admin is held in global $post_type variable you can check.

  2. Late answer

    Translation filters only

    There’re default filters for the title and the content, but none for the excerpt windows. So you basically got to options: Remove the default meta box and add a new (altered) one OR filter the string via the gettext filter.

    Meta Box

    You basically know the concept of removing a meta box (if not, just search on this very site for it). Then just add a new meta box that is exactly the same, but with a slightly altered UI in your custom callback.

    Here’s the original from core as reference:

    function post_excerpt_meta_box($post) {
    ?>
    <label class="screen-reader-text" for="excerpt"><?php _e('Excerpt') ?></label><textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt; // textarea_escaped ?></textarea>
    <p><?php _e('Excerpts are optional hand-crafted summaries of your content that can be used in your theme. <a href="http://codex.wordpress.org/Excerpt" target="_blank">Learn more about manual excerpts.</a>'); ?></p>
    <?php
    }
    

    Gettext

    The point with this is, that this filter triggers for each and every translatable string in the UI (which is a lot). In below plugin you’ll see how to alter the post types default title placeholder, the default content and how to hook in as late as possible to alter this string – and then im. remove the filter so it doesn’t slow down every later filter.

    <?php
    /** Plugin Name: (#72418) "kaiser" Alter Post Type UI strings */
    
    if ( ! class_exists( 'WPSE72418_alter_ptui_strings' ) )
    {
        add_action( 'plugins_loaded', array( 'WPSE72418_alter_ptui_strings', 'init' ) );
    class WPSE72418_alter_ptui_strings
    {
        static protected $instance;
    
        public $post_type = 'post';
    
        public $to_replace = 'Excerpts are optional hand-crafted summaries of your content that can be used in your theme. <a href="http://codex.wordpress.org/Excerpt" target="_blank">Learn more about manual excerpts.</a>';
    
        static public function init()
        {
            null === self :: $instance AND self :: $instance = new self;
            return self :: $instance;
        }
    
        public function __construct()
        {
            add_action( 'init', array( $this, 'add_post_type' ) );
    
            add_filter( 'enter_title_here', array( $this, 'alter_title_string' ), 10, 2 );
    
            add_filter( 'default_content', array( $this, 'add_editor_default_content' ) );
    
            add_action( 'admin_menu', array( $this, 'add_excerpt_note_filter' ) );
        }
    
        public function alter_title_string( $title, $post )
        {
            if ( $this->post_type !== $post->post_type )
                return $title;
    
            return $title = __( 'Enter TITLE name here', 'your_textdomain' );
        }
    
        public function add_editor_default_content( $content )
        {
            if ( $this->post_type !== get_current_screen()->post_type )
                return $content;
    
            return __( 'Enter the POST TYPES long description here.', 'your_textdomain' );
        }
    
        public function add_excerpt_note_filter( $post )
        {
            add_filter( 'gettext', array( $this, 'alter_excerpt_mb_note' ), 10, 3 );
        }
    
        public function alter_excerpt_mb_note( $l10n, $string, $domain )
        {
            // Remove when not on the needed post type page
            if (
                ! is_null( get_current_screen() )
                AND $this->post_type !== get_current_screen()->post_type 
                )
            {
                remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
                return;
            }
    
            // Remove when done
            if ( $this->to_replace === $string )
            {
                remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
                return __( 'NEW FOOTNOTE', 'your_textdomain' );
            }
    
            return $l10n;
        }
    } // END Class WPSE72418_alter_ptui_strings
    
    } // endif;
    
  3. Thanks for this. It is also possible these days to apply a filter to the Title prompt using enter_title_here. Enjoy!

    add_filter ('enter_title_here', your_filter);
    

    As you’d expect.