douxun4173 2017-12-09 02:30 采纳率: 0%
浏览 48
已采纳

将父级作为当前ID无效

I'm using the wp dropzone plugin trying to tweak things a little to create a a front end image uploader that sets the current post as the attachments parent.

The plugin as is works really well but by default no matter where I locate the uploader any attachments are marked as un attached in my media library.

I can't for the life of me work out why this isn't working as I've tried all standard calls to get the current post id and set this as the parent.

The full php plugin file is quite extensive so I've included the core section which operates the attachment insertion. See my progress what I have below.

Importantly if I set the post parent as the actual id number; '240' for example it attaches to the called post. I'm looking to attach this to the current post.

/**
* Handle ajax file upload to media library.
*
* @since    1.0.0
*/
function wpday_dz_ajax_upload_handle() {
if (!empty($_FILES) && wp_verify_nonce($_REQUEST['wpday_dz_nonce'],   'wpday_dz_protect')) {
    // Including file library if not exist
    if (!function_exists('wp_handle_upload')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }

    // Uploading file to server
    $movefile = wp_handle_upload($_FILES['file'], ['test_form' =>  false]);



    // If uploading success & No error
    if ($movefile && !isset($movefile['error'])) {
        $filename = $movefile['file'];
        $filetype = wp_check_filetype(basename($filename), null);
        $wp_upload_dir = wp_upload_dir();

        $attachment = array(
            'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
            'post_mime_type' => $filetype['type'],
            'post_parent' => $post->ID,
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit',
        );

        // Adding file to media
        $attachment_id = wp_insert_attachment($attachment, $filename);

        // If attachment success
        if ($attach_id) {
            require_once ABSPATH . 'wp-admin/includes/image.php';

            // Updating attachment metadata
            $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
            wp_update_attachment_metadata($attach_id, $attach_data);
        }

        $message['error'] = 'false';
        $message['data'] = wp_get_attachment_url($attach_id);   
    } else {
        $message['error'] = 'true';
        $message['data'] = $movefile['error'];
    }

    wp_send_json($message);
}
}
      add_action('wp_ajax_wpday_dz', 'wpday_dz_ajax_upload_handle');
      add_action('wp_ajax_nopriv_wpday_dz',  'wpday_dz_ajax_upload_handle');

EDIT:

Have tried:

$id = get_the_ID();

    // If uploading success & No error
    if ($movefile && !isset($movefile['error'])) {
        $filename = $movefile['file'];
        $filetype = wp_check_filetype(basename($filename), null);
        $wp_upload_dir = wp_upload_dir();

        $attachment = array(
            'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
            'post_mime_type' => $filetype['type'],
            'post_parent' => $id,
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit',
        );

Still returns unattached

  • 写回答

1条回答 默认 最新

  • duanhuang2804 2017-12-09 02:49
    关注

    I downloaded WP Dropzone and had a look at the code. AFAICT the function you are working on is called via AJAX, once a file is dropped/uploaded. In that case, there is no "post" in the current request - you're in an AJAX file unrelated to any post.

    So the question becomes how to get the ID of the post that called the current AJAX request. I found this SO answer which answers that:

    $url     = wp_get_referer();
    $post_id = url_to_postid( $url ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗