0

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?

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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