How to access my uploaded images via Wordpress Media Uploader? - php

It is my first time trying to use Wordpress Media Uploader inside my theme but I am having difficulties accessing my uploaded images. Here is the problem: I can upload my images but I don't know how to retrieve them. For example when i click my upload button it uploads my image but my textfield is blank (it should display path to my uploaded image) so I can use it inside my theme like so :<?php echo $options['body_background'];?>
My functions inside options-page.php look's like this:
//Heading Text (works fine and i can echo value out...)
function text_heading_setting(){
$options = get_option('plugin_options');
$get_options = $options['text_heading'];
echo '<input type="text" name="plugin_options[text_heading]" value="'.$get_options.'" >';
}
//Menu Image upload field (can upload image but can't echo its path)
function body_background_setting(){
$options = get_option('plugin_options');
$get_options_menu = $options['menu_background'];
echo '<div class="uploader">
<input type="text" name="plugin_options[menu_background]" id="menu_image_bg" value="'.$get_options_menu.'" />
<input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" />
</div>';
}
//Body Image upload field (can upload image but can't echo its path)
function body_background_setting(){
$options = get_option('plugin_options');
$get_options_body_bg = $options['body_background'];
echo '<div class="uploader">
<input type="text" name="plugin_options[body_background]" id="menu_image_bg" value="'.$get_options_body_bg.'" />
<input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" />
</div>';
}
Uploader js looks like this:
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('.button').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$("#"+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$('.add_media').on('click', function(){
_custom_media = false;
});
});
Screenshot of my problem:
http://vasinternetposao.com/wordpressdevelopment/imgupload.png
Can someone help me out so I can access my uploaded images?

try adding esc_url() to the value
//Menu Image upload field (can upload image but can't echo its path)
function body_background_setting(){
$options = get_option('plugin_options');
$get_options_menu = $options['menu_background'];
echo '<div class="uploader">
<input type="text" name="plugin_options[menu_background]" id="menu_image_bg" value="'.esc_url($get_options_menu).'" />
<input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" />
</div>';
}
//Body Image upload field (can upload image but can't echo its path)
function body_background_setting(){
$options = get_option('plugin_options');
$get_options_body_bg = $options['body_background'];
echo '<div class="uploader">
<input type="text" name="plugin_options[body_background]" id="menu_image_bg" value="'.esc_url($get_options_body_bg).'" />
<input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" />
</div>';
}

Related

html upload file(image) in one click

I'd like to upload a file in one click, so I tried to combine two click events in one, but the $_FILE variable does not load the image, here's my code :
<form target='_self' action='upload.php' method='post' enctype='multipart/form-data'>
<div class = 'testocentrato'>
<input style='display:none' type='file' accept='.jpg' name='file' id='file'/>
<input style='display:none' type='submit' id='caricaimmagine' name='caricaimmagine' />
<input class='inputfile' type='button' value='Scegli file da PC' onclick='document.getElementById('file').click(); document.getElementById('caricaimmagine').click();' />
<input style='display:none' type='submit' />
<input class='inputfile' type='submit' name='eliminaimmagine' onclick='document.getElementById('eliminaimmagine').click();' value='".$lang['TASTO_ELIMINA_FOTO']."' />
<input type='hidden' name='id_utente' value='".$user['id']."' />
</form>
It is possible to upload images with one click with ajax, check https://makitweb.com/how-to-upload-image-file-using-ajax-and-jquery/.
here is my html code I used the label tag so when I click the upload icon the select file windows will appear.
<form method="post" enctype="multipart/form-data">
<label class='foto' for="imgid">
<img id="photo" src="images/photo.png">
<p>Foto</p>
</label>
<input type="file" value="chosen File" id="imgid" name="img" accept="image/*">
<img class="previewimg" src="" width="100" height="100">
<span id="showImagePath" ></span>
<button class="btn btn-primary" id="toPost">Post</button>
</form>
mycss.
#imgid{
display:none;
}
.foto{
float:left;
}
.previewimg{
display:none;
background-color:grey;
}
#imgToPost{
display:none;
}
my Jquery:
I used setInterval() function to make my script run until the image is selected that way is going to be uploaded and the name and the image can be previewed.
$("#photo").click(function(){
setInterval(function(){
var fd = new FormData();
var files = $('#imgid')[0].files;
// Check file selected or not
if(files.length > 0 ){
$(".previewimg").show();
fd.append('file',files[0]);
$.ajax({
url: 'action.php?action=postcontentimage',
type: 'post',
data:fd,
contentType: false,
processData: false,
success: function(result){
var name ="";
if(result != 0){
$(".previewimg").attr("src", result).show();
for(var i=0; i < result.length;i++){
if(i>8){
name += result[i];
}
$("#showImagePath").html(name);
clearInterval();
}
}
}
})
}else{
alert("no image has been selected");
}
}, 2000);
})
my PHP:
<?php
if(isset($_FILES['file']['name'])){
/* Getting file name */
$filename = $_FILES['file']['name'];
/* Location */
$location = "uploads/".$filename;
$imageFileType = pathinfo($location,PATHINFO_EXTENSION);
$imageFileType = strtolower($imageFileType);
/* Valid extensions */
$valid_extensions = array("jpg","jpeg","png");
$response = 0;
/* Check file extension */
if(in_array(strtolower($imageFileType), $valid_extensions)) {
/* Upload file */
if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
$response = $location;
}
}
echo $response;
exit;
}else{
echo 0;
}
?>
Because you are directly clicking on submit before secting the file.
Remove
document.getElementById('caricaimmagine').click();
You need to click that button manually.

jquery window.send_to_editor.. Can't insert File URL in the text field

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 want to submit image(file) with other form element just using php and jquery not ajax

my HTML here
<body>
<form id="myform" method="POST" action="formprocessing.php" enctype="multipart/form-data">
<input type="text" name="first_name" id="first_name" value=""><b/>
<input type="text" name="last_name" id="last_name" value=""><br />
Profile Image: <input name="image" id="image" type="file" /><br />
<input type="submit" name="formsubmit" id="formsubmit" onclick="return false" value="Submit">
</form>
<div id="result"></div>
</body>
****my jquery code bellow here****
$(document).ready(function(){
$('#formsubmit').click(function(){
var name = $('#first_name').val();
var lastname = $('#last_name').val();
var files = $('#image')[0].files[0];
if (name != '' && lastname != '' && filen != '' ) {
$.post("formprocessing.php" , {ufname:name,ufiles:filen},
function(result){
//$("#spans").html("Email are already exist.");
$('#result').html(result);
$('#myform')[0].reset();
});
}
});
});
});
and my php page where i want to display out is bellow
$name = $_POST['ufname'];
$lastname = $_POST['ulname'];
$img= $_FILES['ufiles']['name'];
echo $name;
echo $lastname;
echo $img;
i just want to submit images and other form element just using jquery and php ,my above code worked but the image file was not upload, how can i do that?
if you want to upload image via PHP here is how you can do it
$extension = pathinfo($_FILES['ufiles']['name'], PATHINFO_EXTENSION);
$filename = '123_'.uniqid().'.'.$extension;
move_uploaded_file($_FILES['profile_image']['tmp_name'], '../path/'.$filename);

Customer uploads different files to one product with different responses from the site

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!

Wordpress media buttons don't work without item attached to?

I'm currently developing theme settings for my Wordpress theme, i need media buttons in order the user can attach files for the homepage. This files are totally independent.
This medias buttons dont work except when i display a Wordpress editor, can anyone have a solution cleaner than hide the editor with a css hack ?
Thanks in advance!
Personally I did it like this:
in the form
<label for="upload_image" class="upload">
<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="Choose Image" />
and then to display the current image selected.
<p><strong>Current Image:</strong>
<?php if (get_option('ad_image')){ echo '<br /><img src="'.get_option('ad_image').'" height="150" width="220"';} else{echo 'None Selected';}?>
</p>
and then enqueue this javascript in the options page
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('#upload_image_button').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$("#"+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$('.add_media').on('click', function(){
_custom_media = false;
});
});

Categories