前端上传图片WordPress的 [英] Front End Upload Images Wordpress

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

问题描述

下面的代码是成功地创建一个自定义帖子,并添加元的细节。代码也在将图片上传到网站根目录的上传文件夹中。我坚持创建一个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 [ '文件'];

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

echo'< pre>';
print_r($ warnings);
echo'< / pre>';
出口;

$ b foreach($ FileUploader-> getRemovedFiles('file')as $ key => $ value){
unlink('../ uploads /') $值[ '名称']);

$ b $ 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);

$ b 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/');

}

?>


解决方案

  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'。
$ b

$ files = $ _FILES ['upload_attachment'];



您可以使用此代码将图片上传到后期元


The below code is successfully creating a custom post and adding meta details to it. The code is also dumping uploading images into an uploads folder in the site root. I'm stuck on creating a foreach argument to post the image file paths into the following custom fields as text strings: image_1, image_2, image_3 and 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/');

            }

            ?>

解决方案

 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']);
                        }

                  }
            }


    }

Replace 'upload_attachment' from your input type file name .

$files = $_FILES['upload_attachment'];

and you can upload your image into post meta using this code

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

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