WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the past Media Library window could be called like this:

tb_show("Upload image", "media-upload.php?type=image&TB_iframe=1&width=640&height=520");

This still works in WP3.5. But I'm wondering how to open a new media manager? It's no longer handled by Thickbox - tb_show().

Thanks for any advices!

share|improve this question
up vote 9 down vote accepted

From the article How to use WordPress 3.5 Media Uploader in Theme Options (by codestag.com), as seen in Using the WordPress 3.5 Media Uploader within plugins (by mikejolley.com). Mike Jolley's article has quite some nice tricks.

Important note: if the page where the uploader is going to be used doesn't already have all media JS APIs, then wp_enqueue_media() has to be called.


So, supposing we have a button like this in a custom Meta Box:

<div class="uploader">
      <input type="text" name="settings[_wpse_82857]" id="_wpse_82857" />
      <input class="button" name="_wpse_82857_button" id="_wpse_82857_button" value="Upload" />
</div> 

The following script will call the new uploader and fill up the input text field #_wpse_82857 with the attachment path when Insert into post is clicked.

jQuery(document).ready(function($)
{
    var _custom_media = true,
        _orig_send_attachment = wp.media.editor.send.attachment;

    // ADJUST THIS to match the correct button
    $('.uploader .button').click(function(e) 
    {
        var send_attachment_bkp = wp.media.editor.send.attachment;
        var button = $(this);
        var id = button.attr('id').replace('_button', '');
        _custom_media = true;
        wp.media.editor.send.attachment = function(props, attachment)
        {
            if ( _custom_media ) 
            {
                $("#"+id).val(attachment.url);
            } else {
                return _orig_send_attachment.apply( this, [props, attachment] );
            };
        }

        wp.media.editor.open(button);
        return false;
    });

    $('.add_media').on('click', function()
    {
        _custom_media = false;
    });
});
share|improve this answer
    
Does this work on admin side? – AlxVallejo Feb 4 '13 at 22:03
    
great example . +1 . but is there a reason why this does not work with the array-like formatting of id="settings[_wpse_82857]" – krembo99 Mar 20 '13 at 4:13
    
I'm getting the following error in console: ReferenceError: wp is not defined. Can you please help me if you know what it is? – CodeVirtuoso Apr 10 '14 at 18:52
    
You are really great. – Sarower Jahan Feb 3 at 6:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.