Best way to filter featured image text for a custom post type?

I am trying to change the “Set featured image”, “Remove featured image”, and “Use as featured image” text links of a custom post type for my plugin Meteor Slides.

I found the “admin_post_thumbnail_html” filter hook, and came up with some code that will change “Set featured image” to “Set slide image” for just the slide post type:

Read More
    if ((isset($_GET['post_type']) && $_GET['post_type'] == 'slide') || (isset($post_type) && $post_type == 'slide')) {

    add_filter( 'admin_post_thumbnail_html', 'meteorslides_set_featured', 9999, 1 );

    function meteorslides_set_featured( $content ) {

        return str_replace( 'Set featured image', 'Set slide image', $content );

    }

}

I tried the same thing for “Remove featured image” and it did not work, however I can use this code to make this change for all post types:

    add_filter( 'admin_post_thumbnail_html', 'meteorslides_remove_featured', 9999, 1 );

function meteorslides_remove_featured( $content ) {

    return str_replace( 'Remove featured image', 'Remove slide image', $content );

}

Does anyone have any thoughts as to why filtering “Set featured image” would behave differently than “Remove featured image”?

Changing “Use as featured image” seems to be a bit trickier, there isn’t a filter for this. But I did find a ticket in Trac where someone else was trying to do the same thing, and there was a suggestion to filter the translation string.

This solution worked pretty well, I was able to change the “Use as featured image” text, but again it is for all post types and I’m not sure how to narrow it down:

    add_filter( 'gettext', 'meteorslides_use_featured', 9999, 4 );

function meteorslides_use_featured( $translation, $text, $domain ) {

    $translations = &get_translations_for_domain( $domain );

    if ( $text == 'Use as featured image' ) {

        return $translations->translate( 'Use as slide image' );

    }

    return $translation;

}

The overlaid media window doesn’t seem to “know” that I am loading it from a certain post type, so I’m not sure if this change is doable.

I did find an answer on here that showed how to change these links with jQuery. I’d prefer to use filters, but if I could figure out how to limit that to one post type that’d be alright too!

I’ve been looking around at some other plugins that use featured images but I haven’t been able to find any that have changed all of these strings. Has anyone pulled this off?

Related posts

Leave a Reply

3 comments

  1. Regardless if you go the PHP or jQuery route, I suggest you set up your filters or enqueue your Javascript in the admin_head-post[-new].php or admin_print_scripts-post[-new].php hook. There you can be sure that the global variable $post_type is set, and can check whether it is slide. Since the post thumbnail code is called after these hooks, you can set up your filters in this hook and they will be executed. Something like this:

    add_action( 'admin_head-post-new.php', 'wpse4270_add_filters_for_slide' );
    add_action( 'admin_head-post.php', 'wpse4270_add_filters_for_slide' );
    function wpse4270_add_filters_for_slide()
    {
        if ( 'slide' == $GLOBALS['post_type'] ) {
            add_filter( 'admin_post_thumbnail_html', 'meteorslides_set_featured', 9999, 1 );
            add_filter( 'admin_post_thumbnail_html', 'meteorslides_remove_featured', 9999, 1 );
            add_filter( 'gettext', 'meteorslides_use_featured', 9999, 4 );
        }
    }
    
  2. As brasofilo mentioned you can do a str_replace() on the admin_post_thumbnail_html filter.

    function changeFeaturedImageLinks($content)
    {
        $content = str_replace(__('Set featured image'), __('YOUR_CUSTOM_TEXT'), $content);
        $content = str_replace(__('Remove featured image'), __('YOUR_CUSTOM_TEXT'), $content);
    
        return $content;
    }
    
    add_filter('admin_post_thumbnail_html', 'changeFeaturedImageLinks');
    
  3. I accomplished this by first hooking into the ‘current_screen’ action and making a determination if my post_type was set, then adding the filter. There may be a shorter way but this works if you need to change the text only on a given post type.

    public function set_featured_image_filter(){
        $screen = get_current_screen();
        if( isset($screen->post_type) && $screen->post_type == 'advertisment'){
            add_filter( 'admin_post_thumbnail_html', 'replace_featured_image_text', 10, 1);
        }
    }
    add_action('current_screen', 'set_featured_image_filter');
    
    public function replace_featured_image_text( $content ) {
            return str_replace( 'Set featured image', __('Set Advertisment image', $this->rns_slingshot_plugin), $content );
        }