I am developing my first wordpress plugin. It only needs to allow a user to change a logo in a custom template and change a color scheme in the custom template.
I have made an admin options page and now want to add a field to allow a user to upload an image. How can I upload an image to the wp-content/uploads folder. So far, I have this within a table:
<td><input name="logo_image" type="file" id="logo_image" value="" /></td>
Is this the right approach? If so, how do I direct the file to the right folder? Doesn't wordpress have it's own way of handling file uploads?
Add this code to your global custom option function.
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
<p><strong>Header Logo Image URL:</strong><br />
<img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/>
<input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>">
Upload
</p>
<script>
jQuery(document).ready(function($) {
$('.header_logo_upload').click(function(e) {
e.preventDefault();
var custom_uploader = wp.media({
title: 'Custom Image',
button: {
text: 'Upload Image'
},
multiple: false // Set this to true to allow multiple files to be selected
})
.on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
$('.header_logo').attr('src', attachment.url);
$('.header_logo_url').val(attachment.url);
})
.open();
});
});
</script>
More info
or
Media uploader in theme and plugin
You can use the inbuilt function of wordpress
<?php wp_handle_upload( $file, $overrides, $time ); ?>
This will automatically move the file to the uploads folder
Or
You can write your own PHP function.
Additional Details can be found here -> Wordpress File Upload
Related
I have created a Wordpress file uploader. Its like when i click on add media button it takes me to add media uploader. But when i choose the image it doesn't get inserted into the field. I want whenever i choose an image and click on insert button, image URL should get inserted into the text editor.
var image_field;
jQuery(function($){
$(document).on('click', 'input.select-img', function(evt){
image_field = $(this).siblings('.img');
check_flag=1;
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
window.send_to_editor = function(html) {
imgurl = $('img', html).attr('src');
image_field.val(imgurl);
tb_remove();
}
return false;
});
});
Include jquery library if you dn't have.
https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"
Please include the below jquery code, where you are adding the HTML code.
Jquery:
jQuery(document).ready(function() {
jQuery('#upload_image_button').click(function() {
formfield = jQuery('#upload_image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#upload_image').val(imgurl);
tb_remove();
}
return false;
});
HTML:
<tr valign="top">
<td>Upload Menu Icon:</td>
<td><label for="upload_image">
<input id="upload_image" type="text" size="36" name="menu_icon" value="" required="" />
<input id="upload_image_button" type="button" value="Upload Image" />
<br />Enter an URL or upload an image from media.
</label>
</td>
</tr>
Include the below PHP code in your theme function.php file.
PHP:
function wp_gear_manager_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('jquery');
}
function wp_gear_manager_admin_styles() {
wp_enqueue_style('thickbox');
}
add_action('admin_print_scripts', 'wp_gear_manager_admin_scripts');
add_action('admin_print_styles', 'wp_gear_manager_admin_styles');
Important Note:
Before click insert button. Please confirm the Image URL is displaying in the text filed. Please see the screenshot
I am using Opencart 1.5.6 and I have two file upload options for one product. One option is an image and the other is a video. I am setting it up to show a preview of the image once they upload.
In catalog/controller/product/product.php under public function upload() I have changed the code as follows to get a thumbnail image and change the success message:
if (!$json && is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
$file = basename($filename) . '.' . md5(mt_rand());
// Hide the uploaded file name so people can not link to it directly.
$json['file'] = $this->encryption->encrypt($file);
//ADDED THIS//
$json['thumb'] = $file;
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file);
//CHANGED THIS//
$json['success'] = 'The file "' . basename($filename) . '" has been uploaded.';
}
In catalog/view/theme/<mytheme>/template/product/product.tpl I added a div to place the thumbnail in.
<div class="image-area" style="width:300px;height:215px;overflow:hidden;"></div>
Then at the bottom of the product.tpl I made the following change:
<script type="text/javascript"><!--
new AjaxUpload('#button-option-<?php echo $option['product_option_id']; ?>', {
action: 'index.php?route=product/product/upload',
name: 'file',
autoSubmit: true,
responseType: 'json',
onSubmit: function(file, extension) {
$('#button-option-<?php echo $option['product_option_id']; ?>').after('<img src="catalog/view/theme/default/image/loading.gif" class="loading" style="padding-left: 5px;" />');
$('#button-option-<?php echo $option['product_option_id']; ?>').attr('disabled', true);
},
onComplete: function(file, json) {
$('#button-option-<?php echo $option['product_option_id']; ?>').attr('disabled', false);
$('.error').remove();
if (json['success']) {
alert(json['success']);
$('input[name=\'option[<?php echo $option['product_option_id']; ?>]\']').attr('value', json['file']);
}
if (json['error']) {
$('#option-<?php echo $option['product_option_id']; ?>').after('<span class="error">' + json['error'] + '</span>');
}
$('.loading').remove();
<!-- ADDED THIS -->
<!-- Show thumb -->
$(".image-area").html('<img src="download/' + json.thumb + '"/><h6>Image File Preview</h6>');
}
});
//--></script>
All of the above code works perfectly. When I upload the image it gives a success message that includes the file name and it displays the image in the div that I created for it.
The problem is that when I click the upload button for the video upload and upload an image (I haven't limited this to just upload just video files). It will replace the image preview area from the previous upload with whatever I uploaded to the video area. I know that this is because it accesses the same code.
The HTML for both buttons on the front end is as follows:
<label style="max-width:200px;"> Upload File:</label>
<input id="button-option-227" class="button " type="button" value="Upload File">
<input type="hidden" value="2J5cqKQ0z8g96DwobtxqZfWcS925rtBIl0U3cSmI06f3EEs9E-6m9k_22emKdPkcR9QjwK3zDdW1I7S4vqQZew,," name="option[227]">
<label style="max-width:200px;">Upload Video:</label>
<input id="button-option-228" class="button " type="button" value="Upload File">
<input type="hidden" value="sbG0MtD_ebRQBeO73zWTNWhK8Y1rf-O6L3422G7D6FNjxlo0FM3GhoC5a1HiZk9DYgX5hjUPj5yxogQ67LrK-A,," name="option[228]">
Question:
How can I separate this code so that only the image upload option will access it and the video upload option will access a different code and respond differently?
If there are any other solutions I would appreciate any tips.
I change the following code:
<!-- Show thumb -->
$(".image-area").html('<img src="download/' + json.thumb + '"/><h6>Image File Preview</h6>');
To:
<!-- Show thumb -->
<?php if ($option['product_option_id'] == <<IMAGE OPTION ID>>) { ?>
$(".image-area").html('<img src="download/' + json.thumb + '"/><h6>Image File Preview</h6>');
<?php } ?>
Thanks for the tip shadyyx!
I have atleast 50 fields in my html form and I have integrated flash file uploader for multiple file uploading.
Another reason of used for flash file uploader is we can't get filesize before uploading file in IE. for get the file size in IE i used flash.
EAflashuploader integration works successfully.
But my requirement is litle bit different.
In EAflashuploader there is upload button when you click on that all added file uploaded.
But I want to disable this upload button and upload file should be upload when i click on
Html submit button.
In short i want to upload files through submit button in html instead of upload button in flash.
when form submit i can get all the data in post method.
Project link : http://www.triggergmbh.com/EAFUpload/PHP/simpleupload.php
EAFlashUpload link : http://www.easyalgo.com/examples/eaflashupload/simpleupload.aspx
EAFlashUpload Doc link : http://www.easyalgo.com/support/docs/eaflashupload/
Javascript function
function doSendForm() {
EAFlashUpload.uploadFiles();
document.forms["id_form"].submit();
}
HTML code
<div style="width:450px;height:350;background:#EFEFEF;">
<form name="id_form" id="id_form" method="post" style="margin:0px;padding:0px;">
<div id="EAFlashUpload_holder"></div>
<script type="text/javascript" src="EAFUpload/swfobject.js"></script>
<script type="text/javascript">
var params = {
BGcolor: "#ffffff",
wmode: "window"
};
var attributes = {
id: "EAFlashUpload",
name: "EAFlashUpload"
};
var flashvars = new Object();
var uploadUrl = "simpleupload.php";
if (!document.all) {
uploadUrl = "../" + uploadUrl;
}
flashvars["uploader.uploadUrl"] = uploadUrl;
flashvars["viewFile"] = "EAFUpload/TableView.swf";
flashvars["view.uploadButton.visible"] = "false";
swfobject.embedSWF("EAFUpload/EAFUpload.swf", "EAFlashUpload_holder", "450", "350", "10.0.0", "EAFUpload/expressInstall.swf", flashvars, params, attributes);
function EAFlashUpload_onMovieLoad(errors)
{
if(errors != "")
alert(errors);
}
</script>
<p>First name: <input type="text" name="firstname"></p>
<p>Last name: <input type="text" name="lastname"></p>
<input type="submit" id="id_submit" name="submit" value="submit" onClick="doSendForm();" style="padding:10px;font-size:20px;">
</form>
</div>
EAFlashbutton seems to have flexible configuration options and a Javascript API.
You should be able to 1) hide the Flash upload button and 2) trigger the upload externally via Javascript.
1) hiding the button
Buttons have a visible property, so you should be able to hide them with something like this:
flashvars["view.uploadButton.visible"] = "false";
2) Triggering the upload
Use the uploadFiles() method of the Javascript API.
I am working on a small WordPress widget and I am trying to set it up where the user can upload an image. So far, the necessary scripts are loading but when I click the "Media Library Image" button the image uploader doesn't pop up.
Here is my javascript file:
jQuery(document).ready(function($) {
var formfield = null;
$('#upload_image_button').click(function() {
$('html').addClass('Image');
formfield = $('#wp-ss-image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
var fileurl;
if (formfield != null) {
fileurl = $('img',html).attr('src');
$('#wp-ss-image').val(fileurl);
tb_remove();
$('html').removeClass('Image');
formfield = null;
} else {
window.original_send_to_editor(html);
}
};
});
Here is are my functions to call the scripts:
add_action('admin_print_scripts-widgets.php', 'wp_ss_image_admin_scripts');
function wp_ss_image_admin_scripts() {
wp_enqueue_script( 'wp-ss-image-upload', plugins_url( '/WP-Simple-Social/wp-simple-social-image.js' ), array( 'jquery','media-upload','thickbox' ) );
}
add_action('admin_print_styles-widgets.php', 'wp_ss_admin_styles');
function wp_ss_admin_styles() {
wp_enqueue_style( 'thickbox' );
}
And here is the html to display the input box and image upload button:
<p>Upload Photo: <input id="wp-ss-image" class="widefat" type="text" size="75" name="<?php echo $this->get_field_name( 'image' ); ?>" value="<?php echo esc_url( $image ); ?>" />
<input id="upload_image_button" type="button" value="Media Library Image" class="button-secondary" />
Since all of the necessary files/scripts are loading, could this be a jQuery problem?
If there is any other additional info that would help out please let me know. Thanks for the help.
Best way I found was using live() method even if it deprecated to on() profit.
Worked
jQuery(document).ready(function($) {
$("#testage").live("click", function() {
alert("CLICK!");
});
});
Didn't work
jQuery(document).ready(function($) {
$("#testage").on("click", function() {
alert("CLICK!");
});
});
If anybody has an explication I would be grateful.
I am currently developing a WordPress site which needs a custom content add/edit module. I am looking for an image upload option in wordpress, for eg. in my form there is an input field titled 'Choose Image', when the user clicks on the input field, i want the wordpress media gallery to popup and be able to choose an image from the gallery. and once the user selects an image, the full url to the image will be filled in the input field. I am sure this can be done because i have once found a code for this, but i forgot where i found it. Please help everybody
Thanks in Advance..
Here is code that will do this:
jQuery(document).ready(function() {
var orig_send_to_editor = window.send_to_editor;
jQuery('#upload_image_button').click(function() {
formfield = jQuery(this).prev('input');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
window.send_to_editor = function(html) {
var regex = /src="(.+?)"/;
var rslt =html.match(regex);
var imgurl = rslt[1];
formfield.val(imgurl);
tb_remove();
jQuery('#show_'+formfield.attr('name')).html('<img src="'+imgurl+'" width="" />')
window.send_to_editor = orig_send_to_editor;
}
return false;
});
});
Here is the HTML to go with it:
<input type="hidden" name="upload_image" />
<?php $post_img = get_post_meta($post->ID,'item_image',true); ?>
<input id="upload_image_button" type="button" value="<?php echo (empty($post_img)?'Upload Image':'Change Image') ?>" />
<div id="show_upload_image"><?php echo (!empty($post_img)?"<img src='$post_img' width='100' />":'')?></div>