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:

    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?

3 Answers
3

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 );
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *