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.

I'm trying to put the WP Pluploader into a meta box on my posts page - as per Plupload Intergration in a meta-box? and http://www.krishnakantsharma.com/2012/01/image-uploads-on-wordpress-admin-screens-using-jquery-and-new-plupload/

I got it all working as in the example in the second link. However, I want to put it all into a class file. It stops working if I wrap it all into a Class. And it's all because of:

function g_plupload_action()
{ 
    // check ajax noonce
    $imgid = $_POST["imgid"];
    check_ajax_referer($imgid . 'pluploadan');

    // handle file upload
    $status = wp_handle_upload($_FILES[$imgid . 'async-upload'], array('test_form' => true, 'action' => 'plupload_action'));

    // send the uploaded file url in response
    echo $status['url'];
    exit;
}

It works if this function is outside the Class and called through:

add_action('wp_ajax_plupload_action', "g_plupload_action");

But inside the Class and called with:

add_action('wp_ajax_plupload_action', array($this, 'g_plupload_action'));

and the return data is 0.

What do I need to do in function g_plupload_action() to make it work?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Aha, the problem as that I was calling the wp_ajax_plupload_action from within my page conditional check like so:

function __construct()
{
    global $pagenow;

    $pages = array('post.php', 'post-new.php');

    if (in_array($pagenow, $pages)) :

        add_action('add_meta_boxes', array($this, 'dhf_video_meta_box'));
        add_action('admin_print_scripts', array($this, 'video_meta_js'));
        add_action('admin_head', array($this, 'plupload_admin_head'));
        add_action('admin_print_scripts', array($this, 'video_meta_css'));
        add_action('wp_ajax_plupload_action', array($this, 'plupload_action'));

    endif;
}

You don't want to do it like that. Take the add_action('wp_ajax_plupload_action', array($this, 'plupload_action')); outside of that check and everything works again.

share|improve this answer

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.