Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wp-media-uploader

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wp-media-uploader

Convert any DOM element into a WordPress media uploader input.

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

WordPress Media Uploader

Inpired on amostajo/wordpress-media-uploader, this jquery library converts any DOM element into a media uploader input.

The Media Uploader will pop-up WordPress Media Library, and will allow to upload and or select uploaded media from the library.

Content:

Install

Via npm:

npm install wp-media-uploader

Or download release and add it into your project.

Basic usage

Enqueue

First enqueue script and media dependnecies:

// (1) Make sure WP media gallery is enqueued
wp_enqueue_media();

// (2) Enqueue "Wordpress Media Uploader"
wp_enqueue_script(
    'wp-media-uploader',
    PATH_TO_FILE . '/jquery.wp-media-uploader.min.js',
    [ 'jquery', 'jquery-ui-core' ],
    '1.0.0',
    true
);

If you want the plugin to load select attachments and render them for you, add wp-api as dependency:

// (1) Make sure WP media gallery is enqueued
wp_enqueue_media();

// (2) Enqueue "Wordpress Media Uploader"
wp_enqueue_script(
    'wp-media-uploader',
    PATH_TO_FILE . '/jquery.wp-media-uploader.min.js',
    [ 'jquery', 'jquery-ui-core', 'wp-api' ],
    '1.0.0',
    true
);

Via HTML

Instantiate via HTML by adding attribute role="media-uploader", additionally, you can configure all options via data attributes:

<a role="media-uploader"
    data-editor="my-editor"
>
    Add Media
</a>

Via Javascript

To instantiate via Javascript, first add an element in the DOM:

<a id="my-uploader">Add Media</a>

Then call to wp_media_uploader jQuery plugin:

$( '#my-uploader' ).wp_media_uploader( options );

Options

The following is the list of available jQuery plugin options:

OptionTypeDescription
namestringInput name. Required if this will be used inside a form.
valuestringInput value. Values separated by commas when using the multiple option.
editorstringEditor ID. Default: the plugin will assign a unique ID
targetstringExpected values after, inside or a DOM selector. This identifies where the selected will be rendered. Default: after
renderboolFlag that indicates if selected results should be rendered inside target or not. Default: true
multipleboolFlag that indicates if multiple selection is allowed. Default: false
clearOnSelectionboolFlag that indicates if results in target should be cleared once a new selection is made, if set to false, results will be appended. The plugin will force this option to true when the multiple option is set to false. Default: true
titlestringTitle to display on top of the media uploader modal.
buttonTextstringSelection button text to display inside the media uploader modal.
mimeTypestringExpected values image, video or empty.
sizestringThe image size to use when rendering an image (for example: thumbnail, large or other).
allowCloseboolWether or not to allow modal to close after selection.
successfunctionFunction to use once a selection is made and after rendering.
mediaFilterfunctionFunction to use to filter selected attachments before rendering.
mediaMapfunctionFunction to use to map selected attachments before rendering.
idValueboolWhether or not to use the attachment ID as the input value instead of a url. Default: true
showInputboolWhether or not to display the input. Default: false
inputCssClassstringThe CSS class(es) to assign to the input.
targetCssClassstringThe CSS class(es) to assign to the target.
templateImagestringHTML or a DOM selector. Template to use for image-based attachments. Default: the plugin will use an internal template
templateVideostringHTML or a DOM selector. Template to use for video-based attachments. Default: the plugin will use an internal template
templateFilestringHTML or a DOM selector. Template to use for file-based attachments. Default: the plugin will use an internal template
templateEmbedstringHTML or a DOM selector. Template to use for embed-based attachments (Simple Post Gallery plugin). Default: the plugin will use an internal template
mediaLoadstringExpectes the name of a global (window) function or empty. This is the callable that will be used to load the media when the plugin is ready. Default: the plugin will user WordPress rest api (if avialable) to load the attachment (Only supports ID values).

HTML attributes

The following lists the HTML attributes and their javascript option counter-part:

HTML attrbutesOptionValue type
valuevaluestring
namenamestring
multiplemultipleint (values 1 or 0)
data-editoreditorstring
data-targettargetstring
data-renderrenderint (values 1 or 0)
data-clear-on-selectionclearOnSelectionint (values 1 or 0)
data-titletitlestring
data-buttonmimeTypestring
data-typesizestring
data-sizesizestring
data-allow-closeallowCloseint (values 1 or 0)
data-id-valueidValueint (values 1 or 0)
data-show-inputshowInputint (values 1 or 0)
data-input-classinputCssClassstring
data-target-classtargetCssClassstring
data-media-loadmediaLoadstring
data-template-imagetemplateImagestring
data-template-videotemplateVideostring
data-template-filetemplateFilestring
data-template-embedtemplateEmbedstring

Option: target

This option allows you to define where is the selection going to be rendered.

after

Note: This is the default value.

Target set to after will make the plugin generate a unique container that will be placed right after the uploader. Selection will be rendered inside the generated <div>:

$( '#my-uploader' ).wp_media_uploader();
<button id="my-uploader">Add Media</button>
<!-- Generated "div" target will be placed here -->
inside

Target set to inside will make the plugin render the selection inside its own HTML element:

$( '#my-uploader' ).wp_media_uploader( {
    target: 'inside',
} );
<div id="my-uploader">
    Click to add media here
    <!-- Selection will be rendered here. the text "Click to add media here" will be cleared upon selection -->
</div>
selector

Target set to a DOM selector will allow you to determine where to render the selection:

$( '#my-uploader' ).wp_media_uploader( {
    target: '#my-container',
} );
<button id="my-uploader">Add Media</button>
<div id="my-container"></div>

Option: mediaFilter

This option allows you to filter attachments before they are rendered, for example, the next snippet filters and returns only attachments with url property:

$( '#my-uploader' ).wp_media_uploader( {
    mediaFilter: function( attachment ) {
        return attachment.url !== undefined;
    },
} );

Option: mediaMap

This option allows you to map and modify attachments before they are rendered, for example, the next snippet maps and returns a modified attachment:

$( '#my-uploader' ).wp_media_uploader( {
    mediaMap: function( attachment ) {
        attachment.is_invalid = isNaN( attachment.id );
        attachment.id = 55;
        return attachment;
    },
} );

Option: mediaLoad

This option allows you to set your own custom global function that will handle the initial attachments load, by default the plugin uses wp.api @ media endpoint; the next snippet uses a custom api endpoint to retrieve the attachments:

$( '#my-uploader' ).wp_media_uploader( {
    value: '45,65,77', // IDs separated by comma
    mediaLoad: 'custom_load_media',
} );

/**
 * @param {object} uploader The uploader instance.
 * @param {array}  values   The collection of values.
 */
window.custom_load_media = function( uploader, values )
{
    // Verify
    if ( wp === undefined || wp.api === undefined )
        return;
    // Disable uploader (to prevent selection during loading)
    uploader.$el.prop( 'disabled', true );
    uploader.$el.addClass( 'loading' );
    uploader.$el.attr( 'disabled', 'disabled' );
    // Make request
    wp.apiRequest( {
        path: 'custom_namespace/v1/custom_endpoint',
        method: 'GET',
        data: {
            values: values,
        },
    } )
    .then( function( data ) {
        var attachments = [];
        // Process data
        for ( var i in data ) {
            // This is the minumin media properties expected to fill
            var media = {
                _model: undefined,
                id: data[i].id,
                type: data[i].type,
                url: data[i].url,
            };
            if ( data[i].alt_text )
                media.alt = data[i].alt_text;
            if ( uploader.options.size
                && data[i].sizes
                && data[i].sizes[uploader.options.size]
            )
                media.url = data[i].sizes[uploader.options.size];
            attachments.push( media );
        }
        uploader.render( attachments );
        // Enable uploader
        uploader.$el.prop( 'disabled', false );
        uploader.$el.removeAttr( 'disabled' );
        uploader.$el.removeClass( 'loading' );
    } );
}

You can also stop the plugin from loading media by sending and empty string as parameter. For instance, this is useful if you plan to add load the attachment with PHP.

$( '#my-uploader' ).wp_media_uploader( {
    value: '45,65,77',
    mediaLoad: '',
    target: '#my-container'
} );
<a id="my-uploader">Add Media</a>
<div id="my-container">
    <?php if ( $attachment ) : ?>
        <div id="<?php echo esc_attr( $attachment->ID ) ?>" class="attachment">
            <!-- custom template-->
        </div>
    <?php endif ?>
</div>

IMPORTANT: If you will load the attachment(s) via PHP, make sure the markup is wrapped inside a div with the following attributes id="{attachment_id}" and class="attachment".

Events

List of events:

EventParametersDescription
uploader:readyvalues, uploaderTriggered when the plugin has initialized and it is ready.
uploader:openuploaderTriggered after WordPress media modal is called and opened.
uploader:render.beforeuploaderTriggered before rendering.
uploader:render.afteruploaderTriggered after rendering.
uploader:renderTriggered after rendering (no parameters).
uploader:attachmentsarray, uploaderTriggered after attachments have been selected, filtered and mapped. Before rendering.
uploader:selectionarray, uploaderTriggered after rendering. The array is the raw collection of models returned by WordPress media uploader modal and not the list of attachements proccessed by the plugin.
uploader:clearTriggered after selection has been cleared. Also triggers uploader:attachments and uploader:selection.

Event usage

$( '#my-uploader' ).on( event, handler );

Examples:

$( '#my-uploader' ).on( 'uploader:ready', function() {
    alert( 'Ready!' );
} );

$( '#my-uploader' ).on( 'uploader:attachments', function( event, attachments, uploader ) {
    console.log( attachments );
    uploader.$el.hide();
} );

Methods

List of methods:

MethodParametersDescription
destroyDestroys plugin instance.
clearClears current selection.

Usage example:

$( '#my-uploader' ).wp_media_uploader( 'destroy' );

Templating

Templating using external selector

The best way to explain templating is with a case scenario.

The following snippets will customize the plugin to use jquery-ui-sortable to enabled sorting inside the target, to allow sorting ofrendered results using drag-and-drop. The image template will be replaced to add an extra remove button, so selected media can be removed from selection.

(1) Enqueue sortable and add WP.API dependency

The following sample, will enqueue the plugin, plus wordpress media, jquery-ui-sortable and wp-api.

wp_enqueue_media();
wp_enqueue_script(
    'wp-media-uploader',
    PATH_TO_FILE . '/jquery.wp-media-uploader.min.js',
    [ 'jquery', 'jquery-ui-core', 'jquery-ui-sortable', 'wp-api' ],
    '1.0.0',
    true
);
(2) Uploader initialized via HTML

The following sample will initialize the uploader input via HTML. The PHP value passed as attribute is validated first, to see if is an array or not, and if so, the value is passed as a comma separated list. sortable css class will be added to the generated target. The template inside #gallery-image will be used to render all selected images. data-clear-on-selection will allow for new selection to be appended to the target.

<div id="gallery" class="gallery-container">
    <button role="media-uploader"
        id="gallery"
        type="button"
        class="button"
        name="gallery"
        value="<?php echo esc_attr( is_array( $gallery ) ? implode( ',', $gallery ) : $gallery ) ?>"
        multiple="multiple"
        data-type="image"
        data-editor="gallery"
        data-clear-on-selection="0"
        data-target-css="sortable"
        data-template-image="#gallery-image"
    ><?php echo __( 'Add Media' ) ?></button>
</div>
<script id="gallery-image" type="text/template">
    <div class="attachment">
        <img><input type="text"/>
        <span class="remove">&times;</span>
    </div>
</script>
(3) Init sortable on ready event

The following sample will initialize sortable once the plugin is ready and has generated the target <div>.

$( '#my-uploader' ).on( 'uploader:ready', function( event, uploader ) {
    if ( uploader.$target.hasClass( 'sortable' ) )
        uploader.$target.sortable();
} );
(4) Add remove behavior

The following sample will allow to remove attachments.

$( document ).on( 'click', '.attachment .remove', function( event ) {
    event.preventDefault();
    if ( confirm( 'Do you really want to remove this item?' ) ) {
        $( this ).closest( '.attachment' ).remove();
    }
} );

Templating using attributes

Another way of templating is by using the data-template-image, data-template-video, data-template-file, and data-template-embed attributes.

The plugin will look for the following selectors inside the template to append data:

SelectorBehavior
inputThe plugin will add the media's value in the value attribute of the input element. The plugin will add the name attribute with the respective name.
.inject-media-idThe plugin will put the media's ID inside the element with this selector.
.inject-media-urlThe plugin will put the media's URL inside the element with this selector.
.inject-media-filenameThe plugin will put the media's filename inside the element with this selector.
imgONLY FOR IMAGE AND EMBED The plugin will add the media's URL in the src attribute of the element. If the media has an alt text, the plugin will add the media's alt text in the alt attribute of the element.
sourceONLY FOR VIDEO The plugin will add the media's URL in the src attribute of the element.

Any template passed through as an attribute must be wrapped in an extra <div> wrapper element. For example:

<div><div class="attachment type-file"><span class="inject-media-filename"></span><input type="hidden"/></div><div>

Above, the template is wrapped in a <div> which holds another <div> with the CSS classes attachment type-file, this second <div> is the one that will be used for rendering. Additionally, the media's filename will be injected inside the <span> with the class inject-media-filename and will add the media's value as an attribute on the input element.

There rendered output will look like this:

<div class="attachment type-file" id="123"><span class="inject-media-filename">filename.jpg</span><input type="hidden" value="123"/></div>

Finally, this is how uploader initialization will look like:

<div role="media-uploader"
    name="test"
    multiple="multiple"
    data-editor="test"
    data-template-file='<div><div class="attachment type-file"><span class="inject-media-filename"></span><input type="hidden"/></div><div>'
><?php echo __( 'Add Media' ) ?></div>

License

MIT - (c) 2022 10 Quality Studio.

Keywords

FAQs

Package last updated on 07 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc