Extend 3.5 media uploader plugin to change button name

Figured it out using a different method. Help from here.

$.fn.oeUpload = function(options) {
    // Set default options
    var defaults = {
      'preview' : '.preview-upload',
      'text'    : '.text-upload',
      'button'  : '.button-upload',
      'name'    : 'Choose Image'
    };
    var options = $.extend(defaults, options);
    var uploader;

    $(options.button).click(function(e) {

        e.preventDefault();

        //If the uploader object has already been created, reopen the dialog
        if (uploader) {
            uploader.open();
            return;
        }

        //Extend the wp.media object
        uploader = wp.media.frames.file_frame = wp.media({
            title: options.name,
            button: {
                text: options.name
            },
            multiple: false
        });

        //When a file is selected, grab the URL and set it as the text field's value
        uploader.on('select', function() {
            attachment = uploader.state().get('selection').first().toJSON();
            $(options.text).val(attachment.url);
            $(options.preview).attr('src', attachment.url);
        });

        //Open the uploader dialog
        uploader.open();

    });
}

$('.upload').oeUpload({name: "Choose This Image"});

Leave a Comment