WordPress Media Uploader with size select - php

I want to add an image input to my own WordPress plugin.
For that I use the standard WordPress media-uploader like so:
var custom_uploader;
$('.upload_image_button').click(function(e) {
input = $(this);
e.preventDefault();
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Collage Image',
library: {
type: 'image'
},
button: {
text: 'Choose Collage Image'
},
multiple: false,
displaySettings: true,
displayUserSettings: false
});
custom_uploader.on('select', function() {
attachment = custom_uploader.state().get('selection').first().toJSON();
input.prev('input').val(attachment.url);
});
custom_uploader.open();
});
This works perfect.
I add two more image sizes that were exact for my plugin:
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'collage-large', 460, 660, true );
add_image_size( 'collage-small', 460, 325, true );
}
My problem:
The selector for the image size or better the thumbnail selector is not shown at the media upload form. How do I do that?

You could use the media insertion dialog as shown on the "edit page" site, which adds alignment, link_to and size input fields. To do so add frame: 'post' to your options array:
file_frame = wp.media.frames.file_frame = wp.media({
title: 'Select a image to upload',
button: {
text: 'Use this image',
},
multiple: false,
frame: 'post', // <-- this is the important part
state: 'insert',
});
Instead of listening to the "select" event listen to the "insert" event. This code shows how retrieve the additional properties including the size:
// When an image is inserted, run a callback.
file_frame.on( 'insert', function(selection) {
var state = file_frame.state();
selection = selection || state.get('selection');
if (! selection) return;
// We set multiple to false so only get one image from the uploader
var attachment = selection.first();
var display = state.display(attachment).toJSON(); // <-- additional properties
attachment = attachment.toJSON();
// Do something with attachment.id and/or attachment.url here
var imgurl = attachment.sizes[display.size].url;
jQuery( '#filenameFromURL' ).val( imgurl );
});

I find some where in Internet. May be it useful.
attachment = custom_uploader.state().get('selection').first().toJSON();
With attachment you can working with:
alt
author
caption
compat ( <-- It is Object )
item
meta
date
dateFormatted
description
editLink
filename
height
icon
id
link
menuOrder
mime
modified
name
nonces ( <-- thi is object)
delete
update
proto
orientation
sizes ( <-- this is object)
full ( <-- this is object)
height
orientation
url
width
proto
medium ( <-- this is object)
height
orientation
url
width
proto
thumbnail ( <-- this is object)
height
orientation
url
width
proto
proto ( <-- this is object)
status
subtype
title
type
uploadedTo
url
width
To solve your problem I suggest use with case 20 above:
input.prev('input').val(attchment.sizes.collage-large.url);
Hope this work!

You're VERY close. To make the size selectable within the admin panel, review the add_image_size Codex Entry:
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'your-custom-size' => __('Your Custom Size Name'),
) );
}
So in your case, this should do what you need:
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'collage-large' => __('Collage Large'),
'collage-small' => __('Collage Small'),
) );
}

Related

Changing Woocommerce Product Image thumbnail size

I want to make the gallery thumbnails on the Woocommerce single product page the same size as my main product image. I know I can set the size with this
add_filter( 'woocommerce_get_image_size_gallery_thumbnail', function($size ) {
return array(
'width' => 487,
'height' => 487,
'crop' => 0,
);
});
But I would like to keep small 100x100x Woo thumbnails for the cart page. I read in the Woo docs that you can change which image sized is being used, so I tried
add_filter( 'woocommerce_gallery_thumbnail_size', function( $size ) {
return 'woocommerce_single';
});
but this is not working. Any thoughts why?
There are some hooks available. If you want to change the size using below hook
add_filter( 'wp_get_attachment_image_src', 'your function name', 50, 4 );
Source:
https://docs.woocommerce.com/wc-apidocs/hook-docs.html
https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
I would do it using CSS, setting a specific size for the cart page:
table.shop_table img {
width: 100px;
height: 100px;
}
The correct snippet:
add_filter( 'woocommerce_get_image_size_gallery_thumbnail', function( $size ) {
return 'thumbnail'; //replace 'thumbnail' with required size name or array()
} );

How to add Media Selector to add_settings_field in WordPress?

How can I add Media Selector to add_settings_field in WordPress?
This the extra fields I added to Settings -> General page in WordPress:
/**
* Add more input fields in general settings.
*/
add_action('admin_init', 'extended_general_settings');
function extended_general_settings() {
add_settings_section(
'other_site_details', // Section ID
'Other Site Details', // Section Title
'extended_general_settings_description_callback', // Callback
'general' // What Page? This makes the section show up on the General Settings Page
);
add_settings_field( // Content
'meta_description', // Option ID
'Meta Description', // Label
'extended_generals_setting_textarea_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'other_site_details', // Name of our section
array( // The $args
'meta_description' // Should match Option ID
)
);
add_settings_field( // Keywords
'meta_keywords', // Option ID
'Meta Keywords', // Label
'extended_generals_setting_textarea_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'other_site_details', // Name of our section
array( // The $args
'meta_keywords' // Should match Option ID
)
);
add_settings_field( // Telephone
'telephone', // Option ID
'Telephone', // Label
'extended_general_settings_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'other_site_details', // Name of our section
array( // The $args
'telephone' // Should match Option ID
)
);
add_settings_field( // Email
'email', // Option ID
'Email', // Label
'extended_general_settings_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed
'other_site_details', // Name of our section (General Settings)
array( // The $args
'email' // Should match Option ID
)
);
register_setting('general','meta_description', 'esc_attr');
register_setting('general','meta_keywords', 'esc_attr');
register_setting('general','telephone', 'esc_attr');
register_setting('general','email', 'esc_attr');
}
function extended_general_settings_description_callback() { // Section Callback
echo '<p>Add additional site info below here:</p>';
}
function extended_general_settings_textbox_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text ltr"/>';
}
function extended_generals_setting_textarea_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<textarea rows="6" cols="40" id="'. $args[0] .'" name="'. $args[0] .'" class="regular-text ltr">' . $option . '</textarea>';
}
But I want to add the media selector so that I can select an image from the media library where I have uploaded all my images to.
Is this possible?
Yeah it's definitely possible. I usually use a text input coupled with the builtin WordPress media uploader insert the image URL of an image in the media library to said text field.
First, make sure to enqueue the media uploader script, as well as your own custom script (which in my case I have called my-admin.js):
add_action('admin_enqueue_scripts', 'my_admin_scripts');
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script('my-admin-js', '/the-url-location-for/my-admin.js', array('jquery'));
wp_enqueue_script('my-admin-js');
}
Add the following input on your settings page (you can add it the same way you added your others):
<input id="upload_image" type="text" size="36" name="ad_image" value=<?PHP echo get_option('ad_image'); ?> />
<input id="upload_image_button" class="button" type="button" value="Upload Menu" />
Then you can add the following script inside my-admin.js:
jQuery(document).ready(function($){
var custom_uploader;
$('#upload_image_button').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function() {
attachment = custom_uploader.state().get('selection').first().toJSON();
$('#upload_image').val(attachment.url);
});
//Open the uploader dialog
custom_uploader.open();
});
});
I have personally used this all over the place, however all of this code is based on the example provided on webmaster-source.com.

How to active media gallery in custom media uploader WordPress

I make custom media up loader and its working perfectly on a meta field. In my media up loader there is no gallery option. But now I want to give an option for gallery on it. I search a lot and follow their steps but not succeed.
Here is my Code that working perfectly without gallery option:
var meta_image_frame_gallery;
// Runs when the image button is clicked.
jQuery('#additional_image_1').click(function(e){
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( meta_image_frame_gallery ) {
meta_image_frame_gallery.open();
return;
}
// Sets up the media library frame
meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
title: 'Upload Image',
button: { text: 'Upload Image' },
library: { type: 'image' },
});
Runs when an image is selected.
meta_image_frame_gallery.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
jQuery('#itv_additional_image_1').val(media_attachment.url);
});
// Opens the media library frame.
meta_image_frame_gallery.open();
});
And here is my code for gallery option:
var meta_image_frame_gallery;
// Runs when the image button is clicked.
jQuery('#additional_image_1').click(function(e){
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( meta_image_frame_gallery ) {
meta_image_frame_gallery.open();
return;
}
// Sets up the media library frame
meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
title: 'Upload Image',
button: { text: 'Upload Image' },
multiple: true,
library: { type: 'image' },
});
// Runs when an image is selected.
//meta_image_frame_gallery.on('select', function(){
//
// // Grabs the attachment selection and creates a JSON representation of the model.
// var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();
//
// // Sends the attachment URL to our custom image input field.
// jQuery('#itv_additional_image_1').val(media_attachment.url);
//});
// When an image is selected, run a callback.
meta_image_frame_gallery.on( 'select', function() {
var selection = meta_image_frame_gallery.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
// Do something with attachment.id and/or attachment.url here
jQuery('#itv_additional_image_1').val(selection.url);
});
});
// Opens the media library frame.
meta_image_frame_gallery.open();
});
First I want an option for gallery and second when I create a gallery, its short-code will appear on meta field and then i save it in db.
Please any give any guidance that help me out.
I figure it by more searching and found some links check them. Now gallery option is active in my media up loader.
Here is my Code now:
Just add three more params
frame: "post",
state: 'gallery-library',
multiple: true
in this line meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
Complete code:
meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
title: 'My Gallery',
frame: "post",
state: 'gallery-library',
library: {
type: 'image'
},
multiple: true
} );
And now its working perfectly

Cannot add custom image uploader for custome post type in custom wordpress theme

I am trying to set image uploader for a custom post type on a Theme I am developing.
The aim is to get it to insert the image URL into the textbox of id "meta-image" but instead it inserts the image into the content editor field!
here is the code I have so far: this displays the button and text input:
<?php
global $post;
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-image" class="prfx-row-title"><?php _e( 'Service Icon', 'prfx-textdomain' ); ?></label>
<input type="text" name="meta-image" id="meta-image" value="<?php if ( isset ( $prfx_stored_meta['meta-image'] ) ) echo $prfx_stored_meta['meta-image'][0]; ?>" />
<input type="button" id="meta-image-button" class="button" value="<?php _e( 'Choose or Upload an Image', 'prfx-textdomain' ); ?>" />
</p>
this includes the necessary js for admin area:
/**
* Loads the image management javascript
*/
function prfx_image_enqueue() {
global $typenow;
if( $typenow == 'service' ) {
wp_enqueue_media();
// Registers and enqueues the required javascript.
wp_register_script( 'meta-box-image', get_bloginfo('stylesheet_directory').'/js/meta-box-image.js', array( 'jquery' ) );
wp_localize_script( 'meta-box-image', 'meta_image',
array(
'title' => __( 'Choose or Upload an Image', 'prfx-textdomain' ),
'button' => __( 'Use this image', 'prfx-textdomain' ),
)
);
wp_enqueue_script( 'meta-box-image' );
}
}
add_action( 'admin_enqueue_scripts', 'prfx_image_enqueue' );
and this is the js:
/*
* Attaches the image uploader to the input field
*/
jQuery(document).ready(function(){
// Instantiates the variable that holds the media library frame.
var meta_image_frame;
// Runs when the image button is clicked.
jQuery('#meta-image-button').click(function(e){
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( meta_image_frame ) {
wp.media.editor.open();
return;
}
// Sets up the media library frame
meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title: meta_image.title,
button: { text: meta_image.button },
library: { type: 'image' }
});
// Runs when an image is selected.
meta_image_frame.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
jQuery('#meta-image').val(media_attachment.url);
});
// Opens the media library frame.
wp.media.editor.open();
});
});
any ideas on how to get this actually storing to the proper area?

How can I abort file upload in Plupload?

I'm trying to create a gallery plugin for Wordpress and I'm using Plupload to upload some images.
Before user can uploading images for a new gallery, the gallery name must be added. Since I'm using drag and drop, I need to abort the file upload if the gallery name i empty.
I've tried various solution, but I'm not able to stop the file upload (I think).
At the bottom in my SJ script, I'm doing a check on dragover. Where I want to abort is marked with //HERE FILE UPLOAD MUST ABORT.
But I'm not sure if this is the right place. Can anyone tell me where I can abort the upload on dragover (and drop)?
// PHP code - I'm adding this data here, because I need some WP data
function plupload_init() {
//$image_size = get_option('sim_gallery_max_width_height');
$plupload_arr = array(
'runtimes' => 'html5,silverlight,flash,html4,browserplus,gears',
'browse_button' => 'plupload-browse-button', // will be adjusted per uploader
'container' => 'plupload-upload-ui', // will be adjusted per uploader
'drop_element' => 'drag-drop-area', // will be adjusted per uploader
'file_data_name' => 'async-upload', // will be adjusted per uploader
'multiple_queues' => true,
'max_file_size' => wp_max_upload_size() . 'b',
'url' => admin_url('admin-ajax.php'),
'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
'filters' => array(array('title' => __('Image files'), 'extensions' => 'jpg,jpeg,gif,png')),
'multipart' => true,
'urlstream_upload' => true,
'multi_selection' => false, // will be added per uploader
// additional post data to send to our ajax hook
'multipart_params' => array(
'_ajax_nonce' => "", // will be added per uploader
'action' => 'plupload_action', // the ajax action name
'imgid' => 0 // will be added per uploader
)
);
?>
<script type="text/javascript">
var plupload_config_vars = <?php echo json_encode($plupload_arr); ?>;
</script>
<?php
}
// JS scrip
function init_image_upload() {
if(jQuery('#image-upload').length == 0)
return;
var plupload_config = JSON.parse(JSON.stringify(plupload_config_vars));
var uploader = new plupload.Uploader(plupload_config);
// Control gallery name
control_gallery_name();
uploader.bind('Init', function(up){});
uploader.init();
// a file was added in the queue
uploader.bind('FilesAdded', function(up, files){
});
// Change border color on drop zone
function drop_area_visual_feedback(up) {
var uploaddiv = jQuery('#plupload-upload-ui');
if ( up.features.dragdrop) {
uploaddiv.addClass('drag-drop');
jQuery('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
if(!jQuery('input[name="gallery-name"]').val()) {
jQuery('label.missing-name').removeClass('hidden');
//HERE FILE UPLOAD MUST ABORT
} else {
uploaddiv.addClass('drag-over');
}
}).bind('dragleave.wp-uploader, drop.wp-uploader', function(){
uploaddiv.removeClass('drag-over');
});
} else {
uploaddiv.removeClass('drag-drop');
jQuery('#drag-drop-area').unbind('.wp-uploader');
}
}
}
Have you tried putting your code in the FilesAdded handler, like this ?
// a file was added in the queue
uploader.bind('FilesAdded', function(up, files){
if(!jQuery('input[name="gallery-name"]').val()) {
jQuery('label.missing-name').removeClass('hidden');
up.splice(0);
}
});

Categories