Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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 Nov 18 '10 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 Nov 18 '10 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 Nov 18 '10 at 23:21
    
Cool, I'll try that, thanks! –  JLeuze Nov 19 '10 at 4:27
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 Nov 26 '10 at 19:34
show 1 more comment

1 Answer

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 );
    }
}
share|improve this answer
    
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 Jan 5 '13 at 6:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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