6

Actualy the question says it all. I integrated a media upload box to my admin page and a button with the text 'Insert into post' makes no sense. I rather like to change it to something like 'Use this image'. Anyone got a clue on how you are able to do so?

6 Answers 6

4
add_filter("attribute_escape", "myfunction", 10, 2);
function myfunction($safe_text, $text) {
    return str_replace("Insert into Post", "Use this image", $text);
}

Place in your theme functions file of in a plugin file.

The first usable filter that this button hits is on the function esc_attr(). So what that code will do is find any instance of Insert into Post that is run through esc_attr() and replace it to Use this image. This code might have undesired issues elsewhere though. Maybe there is a language file method someone might know of that might be a better solution than this.

TRY:

add_filter("attribute_escape", "myfunction", 10, 2);
function myfunction($safe_text, $text) {
    return str_replace(__('Insert into Post'), __('Use this image'), $text);
}

Should account for translations.

6
  • I put this in my plugin, however when the media uploader is triggered by a button it will still show 'Insert into post' =/
    – Mark
    Feb 9, 2012 at 16:46
  • Insert into post or Insert into Post notice the difference
    – Scott
    Feb 9, 2012 at 16:51
  • Ah I discovered the flaw. I have a dutch version of WordPress, in the attribute_escape the sentence is already translated so it could not find the Insert into post string.
    – Mark
    Feb 9, 2012 at 16:57
  • @Mark have a look at my update
    – Scott
    Feb 9, 2012 at 17:00
  • Then one last question. It's logic that it will also change the text on the media box of the post and page where Insert into Post does make sense how can I avoid that from happening?
    – Mark
    Feb 9, 2012 at 17:05
6

With WordPress 3.5+ Media Uploader, there's another way. All strings are localized at the bottom of the page with:

<script type='text/javascript'>
/* <![CDATA[ */
var _wpMediaViewsL10n = {
    "url":"URL",
    "addMedia":"Add Media",
    "search":"Search",
    "select":"Select",
    "cancel":"Cancel",
    "selected":"%d selected",
    "dragInfo":"Drag and drop to reorder images.",
    "uploadFilesTitle":"Upload Files",
    "uploadImagesTitle":"Upload Images",
    "mediaLibraryTitle":"Media Library",
    "insertMediaTitle":"Insert Media",
    "createNewGallery":"Create a new gallery",
    "returnToLibrary":"\u2190 Return to library",
    "allMediaItems":"All media items",
    "noItemsFound":"No items found.",
    "insertIntoPost":"Insert into post",
    "uploadedToThisPost":"Uploaded to this post",
    // ET CETERA
    };
/* ]]> */
</script>

On document.ready, we modify the object:

add_action('admin_footer', function()
{
    ?>
    <script type="text/javascript">
        // or without jQuery: http://stackoverflow.com/q/799981
        jQuery(document).ready( function($) {
            _wpMediaViewsL10n.insertIntoPost = 'Gotchya!';
        });
    </script>
    <?php
});
1
  • 1
    Should be the accepted answer it works great!
    – Christina
    Dec 24, 2015 at 4:41
5

Filter 'gettext', take care to catch the target string only by checking the text domain. See this answer for a rather verbose example.

A simplified way:

add_filter( 'gettext', 'wpse_41767_change_image_button', 10, 3 );

function wpse_41767_change_image_button( $translation, $text, $domain )
{
    if ( 'default' == $domain and 'Insert into Post' == $text )
    {
        // Once is enough.
        remove_filter( 'gettext', 'wpse_41767_change_image_button' );
        return 'Use this image';
    }
    return $translation;
}

This is the only way to avoid false positives. A plugin author may use the same string in another location, so you have to verify the text domain.

2

WARNING: using the hook "attribute_escape" is kinda bad. This function is called for ALL text including post titles and content. Using this filter as stated in the accepted answer breaks html entity encoding.

For instance the title Features & Pricing appears as Features & Pricing in the title field. After saving it then shows Features &amp; Pricing. After saving again you get Features &amp;amp; Pricing.

A better alternative might be to use jquery to change the button text.

add_action( 'admin_head', 'admin_head_script' );
function admin_head_script()
{
?>
<script>
    jQuery(document).ready(function($){
        $('input[value="Insert into Post"]').val('Use this Image');
    });
</script>
<?php
}
1

or via CSS

.your-media-frame .media-button-insert{
    font-size:0px;
}
.your-media-frame .media-button-insert:before{
    content:'Insert image';
    font-size:14px;
}
0

The gettext filter enables you to modify text elements on your WordPress site.

add_filter( 'gettext', 'change_button_text');

function change_button_text( $text ) {
    return str_ireplace( 'Insert into post',  'Use',  $text );
}

enter image description here

3
  • you just repeat a previous +5 answer, and your version is actually weaker Jun 28, 2022 at 4:43
  • I disagree. There is an answer with the 'admin_hook', the 'admin_footer' hook, and actually, one with the 'gettext' hook that has a lot of unnecessary code. Also, there is the one with the 'attribute_escape' hook and it is marked as the right answer but it does not actually work. You can ignore the answer, maybe someone else will find it useful. I did for sure. Jun 28, 2022 at 11:21
  • maybe before you disagree with the highest "ranked" user on WPSE it is worth you actually to read the comments added in the answer text ;). That code will change the text only at wordpress core admin and not text which is used by plugins or themes like your code does. Maybe there is one line that is not realy needed there, but I would even claim there needs to be more code there not less to trigger the filter only when on the actual page in question, as the gettext filter is going to be call 100x of times on admin page load (and many times on front-end) and in 99.999% of the cases do nothing Jun 28, 2022 at 13:19

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.