0

I was wondering if there's a hook/function to add a new tab to the Featured image modal window as shows in the picture below:enter image description here

I've found this thread How to add new tab to media upload manager with custom set of images?, but the answers from there didn't work for me.

1 Answer 1

2

WordPress does not have a built-in hook or function on the PHP side that allows you to directly add a new tab to the Media Library Modal window. To achieve this, you need to use JavaScript to modify the media uploader interface.

You can add a custom tab to the WordPress Media Library modal by using JavaScript to extend the media frame. Here’s a quick example:

JavaScript Code:

jQuery(document).ready(function($){
    var originalMediaTabs = wp.media.view.MediaFrame.Select.prototype.browseRouter;
    wp.media.view.MediaFrame.Select.prototype.browseRouter = function(routerView) {
        originalMediaTabs.apply(this, [routerView]);
        routerView.set({
            'new-tab': {
                text: 'New Tab',  // Tab title
                priority: 100
            }
        });
    };

    wp.media.view.MediaFrame.Select.prototype.on('router:new-tab', function() {
        this.content.set(new wp.media.view.AttachmentsBrowser({
            controller: this,
            collection: new wp.media.model.Attachments(),
            selection: this.state().get('selection'),
            model: this.state()
        }));
    });
});

PHP Code:

function my_custom_media_scripts() {
    wp_enqueue_script(
        'my-custom-media-scripts',
        plugin_dir_url(__FILE__) . 'js/admin.js', // Path to your JS file
        array('jquery', 'media-views'), // Dependencies
        null,
        true
    );
}
add_action('admin_enqueue_scripts', 'my_custom_media_scripts');

This JavaScript code adds a new tab called "New Tab" to the Media Library modal window. The PHP code enqueues the JavaScript on admin pages.

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.