admin管理员组

文章数量:1122846

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:

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.

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:

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.

Share Improve this question asked Aug 12, 2024 at 18:24 mikebrsvmikebrsv 101
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: uploadsFeatured Image add tab