Issue with custom media uploader in WordPress - php

I'm trying to create a metabox in WordPress that allows the user to select a PDF file to attach to the post he/she is creating. So far, the metabox shows well, and the media uploader works as expected to certain extent.
The problem is that when the user clicks in the browse button, the default media uploader will show first. If I close that one and click the button again, then the custom uploader will show up and work as expected. Here is the JS code I'm using...
jQuery(document).ready(function($){
// Instantiates the variable that holds the media library frame.
var pdf_file_frame;
// Runs when the image button is clicked.
$('#pdf-lesson-url-button').click(function(e){
// Prevents the default action from occurring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( pdf_file_frame ) {
pdf_file_frame.open();
return;
}
// Sets up the media library frame
pdf_file_frame = wp.media.frames.file_frame = wp.media({
title: pdf_meta.title,
button: { text: pdf_meta.button },
library: { type: 'application/pdf' },
multiple: false
});
// Runs when an image is selected.
pdf_file_frame.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = pdf_file_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
$('#pdf-lesson-url').val(media_attachment.url);
});
// Opens the media library frame.
wp.media.editor.open();
});
});
I have googled the issue but no satidfactory results came out. I might be using the wrong search phrase, but the truth is that I have no idea of what is happening here, especially when once you get the custom dialog to show it works OK.
Any help is highly appreciated.
Thanks to all who read this post.

Try adding .unbind('click') before .click(function()):
$('#pdf-lesson-url-button').unbind('click').click(function(e){
That may work, unbinding the default click function before running your custom one.

I finally solved it and as almost always, it was not that difficult. I copied the code from an example and while they are using a custom media uploader, in the last line of my code the default one is open instead. Changed the line from...
wp.media.editor.open();
to...
pdf_file_frame.open();
and that did it.
Thanks go to #Calvin for making me rethink.

Related

How to show image browse button in tinymce4 and upload image by php?

my tinymce(ver 4.1) "insert/edit image" dialog doesn't have a image browse button, but I want to add the button just look like tinymce official demo shows), can anyone help me?by the way,how can i use PHP to upload image when I click the image button?I need your help~
I am new here,and i am sorry i didn't have enough reputation to post images.
I got it!
I need define a javascript function, called "file_browser_callback",and "file_browser_callback" attribute to the function,for example:
function myCustomFileBrowser(field_name, url, type, win) {
// Do custom browser logic
win.document.forms[0].elements[field_name].value = 'my browser value';
}
tinyMCE.init({
...
file_browser_callback : myCustomFileBrowser
});
See also: http://www.tinymce.com/wiki.php/Configuration3x:file_browser_callback

Replace image with javascript

I'm very new to coding on the web and I'm trying to make something work.
I'm trying to make a little webpage with an easy function to replace an existing image on the page with an image that the users chooses from his own computer. All of this is expected to be done offline. I have however, no idea how..
How do I tackle this?
p.s. With offline I mean, I am expected that this can be done locally without uploading to a server or anything. I am supposed to put this little page on a usb stick so it can be used as a little tool.
Well. you will need to implement file upload functionaility.
you could uses http://www.uploadify.com/
if so then you would use the onUploadSuccess method, to change the image.
when you say offline? do u mean no internet connection, or will the webpage live on a server like a intranet?
............Just to add to my own answer ........
OK, So you need it on a USB. why not install a standalone Server on the USB that way you can run PHP.
http://www.server2go-web.de/index.html
http://www.uwamp.com/en/
$("#file_upload").uploadify({
height : 30,
width : 120,
swf : 'include/fileuploader/uploadify.swf',
uploader : 'include/fileuploader/uploadify.php',
'onUploadSuccess' : function(file, data, response) {
console.log('The file was saved to: ' + data);
$("#img-preview").html("<img src='"+data+"' />");
}
});
I thought I'd show a code example, as this is the idea of StackOverflow. I hope it illustrates how this thing works.
Instead of relying on a set of plugins and libraries you will find out that it is perhaps even easier with native javascript. You can add jQuery to the mix for event handling, etc if you want, it is pretty much standard in the web-dev toolkit anyway.
HTML
First lets add the html for the input and a placeholder img element. You could of course dynamically add the img file with jQuery or native js.
<input id='ourfile' type='file' />
<!-- The image placeholder for our preview -->
<img id='preview' src='' />
Javascript
// Lets cache our two elements of interest.
ourfile = document.getElementById('ourfile');
preview = document.getElementById('preview');
// Create an instance of the 'native' FileReader.
fr = new FileReader();
// When our input triggers change (i.e. image is selected) load the file using the file reader.
ourfile.onchange = function () {
file = ourfile.files[0];
fr.readAsDataURL(file);
}
// Bind to the `onload` event of our FileReader and set the src of the image to the result (base64 of the image).
fr.onload = function(){
preview.src = fr.result;
}
Details
The link in #Akki619's answer shows about details for checking validity of the image, etc.
Fiddle
Here is a link to a working example:
http://jsfiddle.net/rUvUX/4/
This (readAsDataURL) is what you are looking for.
See working example here
In the example attached, you can send the base64 data of your selected image for uploading also.
OUT OF TOPIC HERE: Most of the client are looking for a mobile web app, an app to take picture from phone and send to the server. Not entirely feasible in web apps.
you can use the below javascript to do this:
<script>
function changeImage(newimage)
{
image = document.getElementById('oldimage');
image.src = newimage;
}
</script>

Fancybox onclose put content

I have form with few input box, for example:
FORM START
News -> input
News picture -> input (id="newspics", name="news_picture")
Add -> submit button
"Hyperlink Set picture" -> opening Fancy box window with gallery (table, 5*5, 25 pictures), images must be hyperlinked with unique id
FORM END
I need communication between Fancy box onclose() and my input (news_picture), when user click to picture I need: closing fancy box, putting id number to my input (news picture). Please if anybody help me.
Update:
I solved problem.
I make little php script, fwrite function write id number when user click to addpicture.php?id=$id in fancy box
After this, I get them, code:
'onClosed' : function() {
jQuery.get('aa.txt',function(data){
alert(data);
});
}
If you want to pass php variables into a fancybox callback you can do
$(".fancybox").fancybox({
"onClosed" : function(){
$("#input_selector").val(<?php echo $phpVariable; ?>);
// or add an ID attribute
// $("#input_selector").attr("id", ""+<?php echo $phpVariable; ?>+"");
}
});
... the example above is to get you an idea how to do it, its not a recipe ;)
Bear in mind that onClosed is a callback for fancybox v1.3.4 ... if yours is v2.x use afterClose instead.

Nothing happens on form submission

I applied tinyMCE to a text area in my sites admin area.
Now there is a page "create category" and and page "edit category."
In edit category, there is a drop-down of the categories, I select one and the text area for category description is filled in with AJAX and a tinyMCE function:_
tinyMCE.activeEditor.setContent(responce);
The category description is filled into the text area on which tinyMCE is applied. But when I click submit, NOTHING happens at all.Similarly, on the create category page, there is no drop down, but when you click submit, nothing happens at all.
This problem does not occur when tinyMCE is not applied. But on the edit category page, it was submitting but not filling in the text area with the category description, when instead of
tinyMCE.activeEditor.setContent(responce);
I used
$("#lang_description").html(responce);
in the callback function for jQuery AJAX.
So the main problem is that the forms are not being submitted and that was the story.
Someone suggested to use the tinyMCE function getContent before I post but I dont understand where and how I would do that.
I've faced a situation once like yours and what I did that was first i set in tinyMCE.init
tinyMCE.init({
mode : "exact", // Used exact
elements : "page_content", // I gave the textarea id and name 'page_content'
...
});
Then I've wrote a function as follows
function get_page_content()
{
var ed = tinyMCE.get('page_content');
return ed.getContent();
}
Then inside my form submit event handler/function I just did
$('#page_content').val(get_page_content()); // I populated my textarea (id=page_content) before the form submission
I received the data using $page_content=$_POST['page_content'] in my php script
Update: May be you can use
var ed=tinyMCE.activeEditor.getContent(); // when you didn't set the mode : "exact" in init function
Reference: getContent and setContent
May be not a solution but If this helps then I'll be glad to know. Also notice Sparky672's comment.

How to use TinyMCE custom button, with custom dialog, to add content?

TinyMCE editor lets you assign a custom button to its editor toolbar. I did that and hooked it up to a Flickr account, so a custom dialog box appears with a selection of images.
I want the user to be able to then click any image and have the URL of that image added to the original input, from where the custom TinyMCE button was clicked.
This is the TinyMCE custom button code I have:
setup : function(ed) {
// Add a custom button
ed.addButton('flickr', {
title : 'Add Flickr Image',
image : 'styles/media_icons/flickr.png',
onclick : function() {
// Add your own code to execute something on click
$("div.mediaLibraryPopup .box").load("scripts/loadMediaLibrary.php");
$("div.mediaLibraryPopup").fadeIn("slow").addClass(container);
}
});
}
Then, when you click on an image, I have this jQuery to handle that event:
$("div.mediaLibraryPopup img").live("click", function() {
var url = $(this).attr("src");
$("div.mediaLibraryPopup").fadeOut("slow");
});
I've been reading the TinyMCE documentation and working with it for hours, and I can't figure out how to pass that URL variable back to the TinyMCE "ed" event so that it can add it to the input.
Since I'll be using this on multiple inputs, I can't just hard-code the input class, either. Any thoughts?
I think you need to insert The URL on the Current Cursor Location in the Editor. if that is the case then you can use this command to insert the content:
tinyMCE.execCommand('mceInsertContent',false,'Your Content Goes Here...');
Hope this helps :)

Categories