Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok, so I'm working on a plugin and just ran into a problem.

Here's what I'm doing:
1. Using a metabox with Plupload to upload an image to an upload dir in the plugin folder.
2. Upon upload success a script is called and the image is initialized in jCrop.
3. The cropped image is processed and saved to another dir in the plugin folder using a form with ajax.
4. Upon image processing success the filename of this new, cropped file is passed back.

And here's where I'm stuck. What I want to do now is to use the filename that I passed back and use it for further processing within Wordpress.

Until now everything has been handled without any WP specific code being involved.

What I was thinking about now was to use $.get to pass the filename on to a standalone php file within the plugin folder and use require_once('wp-load.php'); in it to be able to use Wordpress functions. But while reading up on the I came a cross multiple posts that said that you shouldn't go about that way.

So basically, my question is, how do I go from here:

jQuery.ajax({  
    type: "POST",  
    url: plugin_path + "process.php",  
    data: dataString,
    success: function (data) {
        var obj = jQuery.parseJSON(data);
        if (obj.response == 'success') {
            return obj.tempfile; // PASS THIS ON FOR WP INTERACTION
        } else {
            alert('sorry there was an error');
        }
    }
}); 

To here:

$video_thumb_url = obj.tempfile; // THIS IS WRONG, BUT JUST TO PROVE POINT 

$result = media_sideload_image($video_thumb_url, $post_id, 'video_thumbnail');
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));

if(sizeof($attachments) > 0){
    // set image as the post thumbnail
    set_post_thumbnail($post_id, $attachments[0]->ID);
}  

Thanks in advance

P.S. Really had a hard time coming up with a title for this, if you know something better suited, just let me know/edit it, thanks.

share|improve this question
    
Search inside the tag ajax and take a look on how to use the wp_ajax_* or wp_ajax_nopriv_* hooks. –  kaiser Sep 2 '12 at 23:08
    
I did something similar to this in a plug-in that I am working on. What code are you using to handle the AJAX request in PHP/WP? Take a look at my code (particulary lines 14, 15, and 218) to see what I am doing. –  Joseph Sep 2 '12 at 23:12
add comment

1 Answer

up vote 3 down vote accepted

Instead of cooking up your own PHP file and trying to bring in WordPress functionality, put the functionality inside of WordPress using the AJAX hooks that are designed to do that sort of thing.

More info here: http://codex.wordpress.org/AJAX_in_Plugins

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.