Using WordPress ‘media_view_strings’ PHP filter

The media_view_strings is a WordPress PHP filter that allows you to modify the media view strings displayed in the media editor of WordPress.

Usage

To use this filter, you need to add your custom code to the functions.php file of your WordPress theme or child theme. Here’s an example of how to use the media_view_strings filter:

function modify_media_view_strings($strings, $post) {
    // your custom code here
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we created a function called modify_media_view_strings that takes two parameters: $strings and $post. The $strings parameter is an array of media view strings keyed by the name they’ll be referenced by in JavaScript. The $post parameter is a WP_Post object.

Parameters

The media_view_strings filter has two parameters:

  • $strings (array): Array of media view strings keyed by the name they’ll be referenced by in JavaScript.
  • $post (WP_Post): Post object.

More information

For more information on the media_view_strings filter, please see the WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/media_view_strings/

Examples

Modify the “Insert from URL” media view string

function modify_media_view_strings($strings, $post) {
    // Change the "Insert from URL" string to "Insert from Web Address"
    $strings['url']['title'] = __('Insert from Web Address', 'text-domain');
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we modified the “Insert from URL” string in the media editor to “Insert from Web Address” by accessing the $strings array and changing the value of the title key.

Add a custom media view string

function modify_media_view_strings($strings, $post) {
    // Add a custom string to the media editor
    $strings['custom'] = array(
        'title' => __('Custom String', 'text-domain'),
        'button' => __('Custom Button', 'text-domain')
    );
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we added a custom string to the media editor by adding a new key-value pair to the $strings array. The key is the name of the custom string, and the value is an array that contains the title and button strings.

Remove a media view string

function modify_media_view_strings($strings, $post) {
    // Remove the "Set Featured Image" string from the media editor
    unset($strings['setFeaturedImageTitle']);
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we removed the “Set Featured Image” string from the media editor by using the unset function to remove the setFeaturedImageTitle key from the $strings array.

Modify the “Drop files here” media view string

function modify_media_view_strings($strings, $post) {
    // Change the "Drop files here" string to "Drag and drop files here"
    $strings['dropFiles'] = __('Drag and drop files here', 'text-domain');
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

Disable media view string translation

function modify_media_view_strings($strings, $post) {
    // Disable translation for media view strings
    remove_filter('gettext', 'translate_text');
    remove_filter('gettext_with_context', 'translate_text');
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we disabled translation for media view strings by removing the translate_text function from the gettext and gettext_with_context filters. This can be useful if you want to display media view strings in a language other than the one set for your WordPress site.

function modify_media_view_strings($strings, $post) {
    // Change the "Create Gallery" string to "Create Image Gallery"
    $strings['createGalleryTitle'] = __('Create Image Gallery', 'text-domain');
    return $strings;
}
add_filter('media_view_strings', 'modify_media_view_strings', 10, 2);

In this example, we modified the “Create Gallery” string in the media editor to “Create Image Gallery” by accessing the $strings array and changing the value of the createGalleryTitle key.