Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
wp-media-picker
Advanced tools
This jQuery plugin for WordPress can be used to transform an input field into a flexible and compatible media field with attachment selection and preview.
This jQuery plugin for WordPress can be used to transform an input field into a flexible and compatible media field with attachment selection and preview.
The preferred method to install this package is to use Bower.
bower install wp-media-picker
To include the script and stylesheet, enqueue the script and stylesheet like so:
<?php
wp_enqueue_media();
wp_enqueue_script( 'wp-media-picker', 'PATHTOMEDIAPICKER/wp-media-picker.min.js', array( 'jQuery', 'jquery-ui-widget', 'media-editor', 'underscore' ), '0.7.0', true );
wp_enqueue_style( 'wp-media-picker', 'PATHTOMEDIAPICKER/wp-media-picker.min.css', array(), '0.7.0' );
Make sure to use the proper hook to enqueue the assets, for example in the admin_enqueue_scripts
hook. Furthermore the dependencies in the above code sample must explicitly be included, otherwise the plugin will not work.
To turn your raw and boring input fields into really exciting media picker fields, you simply need to run the main plugin function wpMediaPicker()
on your jQuery elements. For example:
jQuery( '.custom-media-field' ).wpMediaPicker();
If you want to store media URLs in the fields (by default the plugin stores media IDs), you need to implement an additional AJAX function somewhere in your code (for example in your theme's functions.php
) for the plugin to work properly - don't worry, it's a pretty simple function. You could just copy the following code snippet:
<?php
function mytheme_ajax_get_attachment_by_url() {
if ( ! isset( $_REQUEST['url'] ) ) {
wp_send_json_error();
}
$id = attachment_url_to_postid( $_REQUEST['url'] );
if ( ! $id ) {
wp_send_json_error();
}
$_REQUEST['id'] = $id;
wp_ajax_get_attachment();
die();
}
add_action( 'wp_ajax_get-attachment-by-url', 'mytheme_ajax_get_attachment_by_url', 15 );
The plugin supports numerous settings so that you can tweak how your fields work. There are two ways to apply settings to a field: Either specify the settings (as an object) when initializing the plugin in Javascript, or apply them as data attributes on the field.
Here you find a list of all available settings:
store
:
query
:
{ type: 'image' }
).multiple
:
filterable
:
searchable
:
editable
:
displaySettings
:
displayUserSettings
:
change
:
clear
:
label_add
:
label_replace
:
label_remove
:
label_modal
:
label_button
:
There are a number of methods that you can call by using a construct like jQuery( '{{SELECTOR}}' ).wpMediaPicker( '{{NAME_OF_FUNCTION}}' )
.
open()
:
close()
:
attachment()
:
value()
:
store: 'id'
) or a string (for store: 'url'
) (only for the setter functionality)frame()
:
Besides a regular change
event fired on the input when its value has changed, there are two custom events that are fired on the document:
wpMediaPicker.updateField
:
wpMediaPicker.insert
:
I'm always grateful for contributions, whether it is about enhancements or bugfixes, especially since the plugin is at an early stage. If you encounter bugs, compatibility issues or totally missing functionality that must be in this plugin, I would appreciate if you created an issue. Or even better, if you can, do it yourself and open a pull-request.
FAQs
This jQuery plugin for WordPress can be used to transform an input field into a flexible and compatible media field with attachment selection and preview.
The npm package wp-media-picker receives a total of 11 weekly downloads. As such, wp-media-picker popularity was classified as not popular.
We found that wp-media-picker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.