To add a new tab to the WordPress media library modal window, you can use the wp.media
JavaScript API and the wp.media.view.Settings
view. Here’s an example of how to do it:
(function($){ 'use strict'; $(document).ready(function(){ var media = wp.media; media.view.Settings.CustomTab = media.view.Settings.extend({ className: 'custom-settings', template: media.template('custom-tab-settings'), initialize: function() { media.view.Settings.prototype.initialize.apply(this, arguments); } }); media.view.Modal.prototype.on('open', function(){ if( this.content.get('customSettings') ) { this.content.set( 'customSettings', new media.view.Settings.CustomTab({model: this.content}), {silent: true} ); } }); }); })(jQuery);
- First, create a new JavaScript file and enqueue it in your plugin or theme. This code should be added to a JavaScript file that is loaded on the admin side.
-
The code above adds a new view to the
wp.media.view.Settings
namespace calledCustomTab
. This view sets theclassName
andtemplate
properties, which define the appearance and content of the new tab. -
The
initialize
method calls the parentinitialize
method, which sets up the default behavior for the settings view. -
In the
media.view.Modal
prototype, we listen to theopen
event, and check if thecustomSettings
property exists. If it does, we set it to a new instance of theCustomTab
view, passing in the currentcontent
model. -
Finally, create a template with the content of the new tab. The template should be saved in a separate file and loaded in the same way as the JavaScript file above. Here’s an example template for the custom tab:
<script type="text/html" id="tmpl-custom-tab-settings"> <h2><?php esc_html_e( 'Custom Tab', 'textdomain' ); ?></h2> <p><?php esc_html_e( 'This is some custom content for the new tab.', 'textdomain' ); ?></p> </script>
This code adds a new tab to the media library modal window with the title “Custom Tab” and some example content. You can customize the appearance and content of the tab by modifying the className
and template
properties in the JavaScript code and the HTML template.