4

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?

5
  • What's wrong with the jQuery method, if you hook onto the edit screen conditionally it will only run for your selected post type.. NOTE: Every screen in the admin loads jQuery..(and i mean literally, even plugin pages).. unless you have specific non-js user requirements i'd have thought jQuery would be the "easy" route..(can help you with jQuery if necessary).. ELSE.. your options are limited to gettext filters (of which, can you not globalise $post_type(or $typenow) and check the current type?).
    – t31os
    Commented Nov 18, 2010 at 22:33
  • Nothing particularly wrong with the jQuery method. I use jQuery for interactivity and animation, using it for editing content like this just seems weird. But if it's easier and it works, why not! Ah, I did forget to globalise the $post_type, but it doesn't seem to have any effect in this case.
    – JLeuze
    Commented Nov 18, 2010 at 22:48
  • I'm not immediately familiar with the gettext hook, but perhaps try the $typenow var and see if that's set.. It's all just a matter of working with the hooks available, and i'm not sure there's one specifically for that piece of text(aside from the gettext one)..
    – t31os
    Commented Nov 18, 2010 at 23:21
  • @JLeuze: If you found a solution by yourself, please also post it here as an answer, so others can use it and we know you're not looking for it anymore.
    – Jan Fabry
    Commented Nov 25, 2010 at 19:45
  • 1
    @Jan: I haven't been able to get all the filters to work for just one post type and I haven't had a chance to try doing this with jQuery yet.
    – JLeuze
    Commented Nov 26, 2010 at 19:34

3 Answers 3

5

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 );
    }
}
1
  • It seems to me that this would be just a matter of doing a str_replace in the $content parameter of admin_post_thumbnail_html. :::: I'm digging into this filter and it takes 2 parameters, ( $content, $post_id ), being the first a plain string.
    – brasofilo
    Commented Jan 5, 2013 at 6:05
1

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');
2
  • Perfect! compliments this trick which allows for changing the feat. image metabox title.
    – wkw
    Commented May 12, 2015 at 20:58
  • How can I use it for a special post type? Commented Mar 12, 2016 at 19:52
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.