The Media Library in WordPress has “Insert from URL” link to the left. It accepts image URLs. It’s not said anywhere but if you paste YouTube URL or <iframe>
code then it will insert it too. It just won’t show the preview like it does with image URLs so you can’t tell if the code you typed in is valid or not.
- I’d like to edit this page and make it show previews of valid WordPress Embeds and of valid
<iframes>
. I want it to just show basic information that the embed code was recognized, is valid and can be inserted. I can write all the AJAX requests to validate URLs but I need help withwp.media
object. I have no idea how to update that page or replace it with my own page? So far I’ve found /wp-includes/js/media-editor.js where on line 1054 it saysCalled when 'Insert From URL' source is not an image. Example: YouTube url.
. - Secondly, once it’s already done, I’d like to be able to call something like the following piece of code to open the Media Library with “Insert from URL” page selected and with the code added (for users to modify the existing code).
The code below is a working example of how to bring a window for editing images but I need this for “Insert from URL”:
frame = wp.media({
frame: 'image',
state: 'image-details',
metadata: { 'url': 'some-url' }
});
frame.open();
To modify the behavior of the “Insert from URL” feature in the WordPress Media Library, you will need to write a custom JavaScript code that extends the existing functionality of the media editor. Here are the steps you can follow:
Enqueue your custom JavaScript file to WordPress using the wp_enqueue_script() function. You can do this by adding the following code to your theme’s functions.php file or to a custom plugin:
In your custom JavaScript file, you can modify the behavior of the “Insert from URL” feature by hooking into the wp.media.controller.Library object. Here’s an example of how you can modify the behavior to show previews of valid WordPress Embeds and valid :
The code above extends the insert() method of the wp.media.controller.Library object to check if the URL is a valid WordPress Embed or . If it is, the code will show a preview of the valid Embed or iframe in the media modal. The attachment will then be inserted as usual, and the media modal will be closed.
To open the Media Library with the “Insert from URL” page selected and with the URL added, you can use the following code:
This code creates a new media frame with the title “Insert from URL” and the type set to “url”. It then sets the ‘states’ option to an array with a single item, which is a new instance of the wp.media.controller.Library object. This object sets the ‘library’ option to wp.media.query({ type: ‘url’ }), which means it will only show the “Insert from URL” page in the media library.
The insert() method of the wp.media.controller.Library object is overridden to extract the URL from the input field and insert an with the given URL into the editor.
Finally, the ‘open’ event of the media frame is hooked to set the default URL value to “https://www.youtube.com/watch?v=dQw4w9WgXcQ” for demonstration purposes.
Note that the above code is just an example, and you may need to adjust it to fit your specific needs. Also, keep in mind that modifying core WordPress files is generally not recommended, as it can lead to compatibility issues and make it harder to update your site. It’s better to use a plugin or child theme to add your custom functionality.