【问题标题】:Front End Upload Images Wordpress前端上传图片 Wordpress
【发布时间】:2017-11-29 02:32:01
【问题描述】:

下面的代码成功地创建了一个自定义帖子并向其添加元详细信息。该代码还将上传图像转储到站点根目录中的上传文件夹中。我坚持创建一个 foreach 参数来将图像文件路径作为文本字符串发布到以下自定义字段中:image_1、image_2、image_3 和 image_4。

            <?php

            if(isset($_POST['url']) && $_POST['url'] == ''){

                require('../wp-load.php'); 

                $a = $_POST['a'];
                $b = $_POST['b'];
                $c = $_POST['c'];
                $d = $_POST['d'];
                $e = $_POST['e'];

                include('../src/class.fileuploader.php');

                $FileUploader = new FileUploader('files', array(
                    'uploadDir' => '../uploads/',
                    'title' => 'name'
                ));

                $data = $FileUploader->upload();

                if($data['isSuccess'] && count($data['files']) > 0) {

                    $uploadedFiles = $data['files'];
                }
                if($data['hasWarnings']) {
                    $warnings = $data['warnings'];

                    echo '<pre>';
                    print_r($warnings);
                    echo '</pre>';
                    exit;
                }

                foreach($FileUploader->getRemovedFiles('file') as $key=>$value) {
                    unlink('../uploads/' . $value['name']);
                }

                $my_post = array(
                    'ID' => '',
                    'post_title'    => wp_strip_all_tags($_POST['a']),
                    'post_content'  => $_POST['d'],
                    'post_status'   => 'draft',
                    'post_type'     => 'custompost'
                );

                $post_id = wp_insert_post($my_post);


                add_post_meta($post_id, 'b', $b, true);
                add_post_meta($post_id, 'c', $c, true);
                add_post_meta($post_id, 'e', $e, true);
                add_post_meta($post_id, 'image_1', $image_1, true);
                add_post_meta($post_id, 'image_2', $image_2, true);
                add_post_meta($post_id, 'image_3', $image_3, true);
                add_post_meta($post_id, 'image_4', $image_4, true);

            header('Location: http://www.example.com/thank-you/');

            } else {

            header('Location: http://www.example.com/thank-you/');

            }

            ?>

【问题讨论】:

标签: php wordpress foreach meta


【解决方案1】:

我设法用页面上的一些 jQuery 解决了这个问题。不过感谢您的帮助。

【讨论】:

    【解决方案2】:
     if(isset($_POST['pj'])){
            global $wpdb;
                $files = $_FILES['upload_attachment'];
                foreach ($files['name'] as $key => $value) {
                    if ( ! function_exists( 'wp_handle_upload' ) ) {
                        require_once( ABSPATH . 'wp-admin/includes/file.php' );
                    }
                      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]
                            );
    
                            $upload_overrides = array( 'test_form' => false );
                            $movefile =  wp_handle_upload($file, $upload_overrides );
                            if ( $movefile && !isset( $movefile['error'] ) ) { 
                                    //echo $movefile['url'];
    add_post_meta($postid , 'images' , $movefile['url']);
                            }
    
                      }
                }
    
    
        }
    

    从您的输入类型文件名中替换“upload_attachment”。

    $files = $_FILES['upload_attachment'];

    您可以使用此代码将您的图片上传到帖子元中

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多