Question

I'm using Thickbox with WordPress, after uploading Thickbox sends data back to editor (upload field), the whole jQuery code looks just like here:

jQuery(document).ready(function() {

   jQuery('#upload_image_button').click(function() { //user clicks upload button
     tb_show('', 'media-upload.php?type=image&TB_iframe=true'); //Thickbox pop-ups
     return false;
    });

    window.send_to_editor = function(html) { //here's our callback function
     imgurl = jQuery('img',html).attr('src');
     jQuery('#upload_image').val(imgurl); //that's how we send something to front-end
     //how to send "imgurl" to back-end within this function?
     tb_remove(); //closes Thickbox
    }

 });

I'm wondering what's the optimal way of sending variable from JS to PHP (I want to send it to WP function so it will got registered as an "option" using update_option('option_name','variableFromJS');.

Keep in mind Thickbox is opening within an iframe, so all I have got is this callback function.

Was it helpful?

Solution

Just do a $.post() back to your PHP page within the function.

window.send_to_editor = function(html) { //here's our callback function
  imgurl = jQuery('img',html).attr('src');
  jQuery('#upload_image').val(imgurl); //that's how we send something to front-end
  //do an AJAX post back to the server (you'll probably want call backs, but this is simple
  $.post('media-upload.php',{ url: "coolURLToImage" });
  tb_remove(); //closes Thickbox
}

Documentation: http://api.jquery.com/jQuery.post/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top