前端多文件上传WordPress [英] Front End Multiple File Upload WordPress

查看:85
本文介绍了前端多文件上传WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图允许用户在网站的前端创建事件(自定义帖子类型).一切都在更新,但是我想让他们上传图像并将其附加到该事件的ACF Gallery字段中.

I'm trying to allow users to create an event (custom post type) on the front end of a website. Everything is updating, but I want to let them upload images and attach them to the ACF Gallery field for that event.

所以rar我有这个:

<input type="file" name="upload_attachment[]" class="files" size="50" multiple="multiple" />

和:

if (!function_exists('wp_generate_attachment_metadata')) {
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}

if ($_FILES) {

    $files = $_FILES['upload_attachment'];
    foreach ($files['name'] as $key => $value) {

        if ($files['name'][$key]) {
            $file = array(
            'name'     => $files['name'][$key],
            'type'     => $files['type'][$key],
            'tmp_name' => $files['tmp_name'][$key],
            'error'    => $files['error'][$key],
            'size'     => $files['size'][$key]
        );

        $attachment_id = media_handle_upload($key, $post_id);

        if ( is_wp_error( $attachment_id ) ) {
            echo('error <br/>');
        } else {
            echo('success!<br />');
        }

        var_dump($attachment_id);

    }
}

但是,它似乎不起作用.基本上,我希望将图像上载到媒体库,然后将图像插入到"ACF画廊"字段中.

But, it doesn't seem to be working. Basically I want the image to be uploaded to the media library, and then insert the image into the ACF Gallery Field.

但是我认为第一步是将其放入媒体库中.

But I think the first step is getting it into the media library.

推荐答案

好吧,我设法弄清楚了,以防万一其他人需要做这样的事情.

Okay, I managed figure it out, incase anyone else needs to do something like this.

在前端:

<input type="file" name="upload_attachment[]" class="files" size="50" multiple="multiple" />
<?php wp_nonce_field( 'upload_attachment', 'my_image_upload_nonce' ); ?>

在php文件中,我将表单发布到:

And in the php file I post the form to, I have this:

if (!empty($_FILES['upload_attachment']['name'][0])) {

        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );


        $files = $_FILES['upload_attachment'];
        $count = 0;
        $galleryImages = array();


        foreach ($files['name'] as $count => $value) {

            if ($files['name'][$count]) {

                $file = array(
                    'name'     => $files['name'][$count],
                    'type'     => $files['type'][$count],
                    'tmp_name' => $files['tmp_name'][$count],
                    'error'    => $files['error'][$count],
                    'size'     => $files['size'][$count]
                );

                $upload_overrides = array( 'test_form' => false );
                $upload = wp_handle_upload($file, $upload_overrides);


                // $filename should be the path to a file in the upload directory.
                $filename = $upload['file'];

                // The ID of the post this attachment is for.
                $parent_post_id = $post_id;

                // Check the type of tile. We'll use this as the 'post_mime_type'.
                $filetype = wp_check_filetype( basename( $filename ), null );

                // Get the path to the upload directory.
                $wp_upload_dir = wp_upload_dir();

                // Prepare an array of post data for the attachment.
                $attachment = array(
                    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                    'post_mime_type' => $filetype['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );

                // Insert the attachment.
                $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

                // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                // Generate the metadata for the attachment, and update the database record.
                $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
                wp_update_attachment_metadata( $attach_id, $attach_data );

                array_push($galleryImages, $attach_id);

            }

            $count++;

            // add images to the gallery field
            update_field('field_535e6a644107b', $galleryImages, $post_id);

        }



    }

此脚本现在允许将多个文件上传到服务器,附加到正在创建的帖子中,并添加到"ACF画廊"字段中.

This script now allow multiple files to be uploaded to the server, attached to the post being created and added to the ACF Gallery field.

这篇关于前端多文件上传WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆