Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 将所选图像文件输入设置为来自wp前端的特征图像_Image_Wordpress_Forms - Fatal编程技术网

Image 将所选图像文件输入设置为来自wp前端的特征图像

Image 将所选图像文件输入设置为来自wp前端的特征图像,image,wordpress,forms,Image,Wordpress,Forms,场景:我允许从前端创建帖子。表单还有四个图像上传字段。我使用下面粘贴的代码进行图像附件和设置帖子缩略图 //insert attachments if ($_FILES) { foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file,$pid); } } //attachment helper function fun

场景:我允许从前端创建帖子。表单还有四个图像上传字段。我使用下面粘贴的代码进行图像附件和设置帖子缩略图

//insert attachments
    if ($_FILES) {
    foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file,$pid);
        }
        } 


 //attachment helper function   
 function insert_attachment($file_handler,$post_id,$setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
        } 
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');

        $attach_id = media_handle_upload( $file_handler, $post_id );
        //set post thumbnail
        if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id); //you will need to comment out this line if you use my solution
        return $attach_id;//you will need to comment out this line if you use my solution
            }
通过此代码设置的后期缩略图/特色图像是最后上载的图像。我试着在谷歌上搜索“来自前端wordpress的set post缩略图”,浏览了很多文章,但没有一篇接近我的要求。我在SE上遇到的大多数帖子都是关于前端发布的,要么是关于设置特色图片,要么是关于多次上传。当我写这个问题的时候,我也检查了所有被提示的问题,以确定之前是否有人问过

如果重要的话,这里是表单中使用的html,非常标准

<input type="file" name="image-one" id="image-one"/>
<input type="file" name="image-two" id="image-two"/>
<input type="file" name="image-three" id="image-three"/>


最后一句话:进度报告中的代码立即为我完成了任务,即。。将第一个图像文件输入设置为特色图像/帖子缩略图,我甚至不确定这是否是最好的方法。我仍然在寻找一个解决方案,它可以提供灵活性,选择任何图像输入字段作为特色图像。如果您决定使用此功能,请记住注释掉原始代码中的倒数第二行和倒数第三行

使用_缩略图_id作为meta_键和attach_id作为meta_值保存帖子不会使该图像成为特色图像,您可以使用它来帮助您。确保你阅读了法典和那里的例子。该功能应与wordpress结合使用,这样您的功能图像将由wordpress正确创建

编辑:
我已经看过你的代码了,我想在这种情况下使用它比使用我的schetch更容易。我添加了一个文件计数和一个参数来插入附件。检查此代码,让我知道发生了什么

例如:

<?php
//insert attachments
if ($_FILES) {
array_reverse($_FILES);
$i = 0;//this will count the posts
foreach ($_FILES as $file => $array) {
    if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
    else $set_feature = 0; //if $i!=0 we are not dealing with the first post
    $newupload = insert_attachment($file,$pid, $set_feature);
    $i++; //count posts
    }
    } 


//attachment helper function   
function insert_attachment($file_handler,$post_id,$setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
    } 
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );
    //set post thumbnail if setthumb is 1
    if ($setthumb == 1) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    return $attach_id;
        }
  ?>

我们可以使用以下功能从前端设置帖子特色图像, 很简单,您需要调用此函数

post\u imgs\u更新($new\u post\u id)

创建新帖子之后。并在functions.php中添加以下函数

if ( ! function_exists( 'post_imgs_update' ) ) :
        function post_imgs_update($new_post_id) {

            $data = array();
            $attachment_ids = array();
            // code for save featured image
            if( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'image_upload' ) ){  // here nonce text is "image_upload" you need to add in form
                    $files = reArrayFiles($_FILES['files']);

                if(!empty($_FILES['files'])){
                    //$i = 0; 
                    $set_featured_img = 0 ;

                    foreach( $files as $file ){
                        if( is_array($file) ){
                            $attachment_id = upload_user_file( $file, basename($file['name']) );
                            if ( is_numeric($attachment_id) ) {
                                if ($set_featured_img == '0') {
                                    set_post_thumbnail($new_post_id, $attachment_id);
                                    $set_featured_img++;
                                    $attachment_ids[] = $attachment_id;
                                }
                                else{
                                    $attachment_ids[] = $attachment_id;
                                }

                            }
                        }
                    }
                    //add to array
                    $mediaurl = array();
                    foreach ($attachment_ids as $img_id) {
                        $imgurl = wp_get_attachment_url( $img_id );
                        $image_gallery[$img_id] = $imgurl;
                    }

                    update_post_meta( $new_post_id, '_stw_property_multi_images', $image_gallery);      // 2nd arg field key
                }

            } 
            else {
                    $data['status'] = false;
                    $data['message'] = __('Nonce verify failed','realestate');
            }

            return $new_post_id;

        }
    endif;

    if ( ! function_exists( 'reArrayFiles' ) ) :
        function reArrayFiles(&$file_post) {
            $file_ary = array();
            $file_count = count($file_post['name']);
            $file_keys = array_keys($file_post);
            for ($i=0; $i<$file_count; $i++) {
                foreach ($file_keys as $key) {
                    $file_ary[$i][$key] = $file_post[$key][$i];
                }
            }
            return $file_ary;
        }
    endif;

    if ( ! function_exists( 'upload_user_file' ) ) :
        function upload_user_file( $file = array(), $title = false ) {
            require_once ABSPATH.'wp-admin/includes/admin.php';
            $file_return = wp_handle_upload($file, array('test_form' => false));
            if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){
                return false;
            }else{
                $filename = $file_return['file'];
                $attachment = array(
                    'post_mime_type' => $file_return['type'],
                    'post_content' => '',
                    'post_type' => 'attachment',
                    'post_status' => 'inherit',
                    'guid' => $file_return['url']
                );
                if($title){
                    $attachment['post_title'] = $title;
                }
                $attachment_id = wp_insert_attachment( $attachment, $filename );
                require_once(ABSPATH . 'wp-admin/includes/image.php');

                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
                wp_update_attachment_metadata( $attachment_id, $attachment_data );
                if( 0 < intval( $attachment_id ) ) {
                    return $attachment_id;
                }
            }
            return false;
        }
    endif;


    function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
        $attachurlarray = array();
        // check to make sure its a successful upload
        if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

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

        $attach_id = media_handle_upload( $file_handler, $post_id );
        return $attach_id;  
    }
如果(!function_存在('post_imgs_update')):
功能post\u imgs\u更新($new\u post\u id){
$data=array();
$attachment_ids=array();
//保存特色图片的代码
如果(isset($_POST['nonce'])和&wp_verify_nonce($_POST['nonce'],'image_upload')){//这里的nonce文本是“image_upload”,您需要在表单中添加
$files=reArrayFiles($_files['files']);
如果(!空($\u文件['FILES'])){
//$i=0;
$set\u特色化\u img=0;
foreach($files作为$file){
if(is_数组($file)){
$attachment_id=upload_user_file($file,basename($file['name']);
如果(是数字($attachment_id)){
如果($set\u-img=='0'){
设置发布缩略图($new\u post\u id,$attachment\u id);
$set__img++;
$attachment\u id[]=$attachment\u id;
}
否则{
$attachment\u id[]=$attachment\u id;
}
}
}
}
//添加到数组
$mediaurl=array();
foreach($img\u id作为附件){
$imgurl=wp\u get\u attachment\u url($img\u id);
$image\u gallery[$img\u id]=$imgurl;
}
更新发布meta($new发布id,$stw属性多个图像',$image\u gallery);//第二个参数字段键
}
} 
否则{
$data['status']=false;
$data['message']=_uuu('Nonce verify failed','realstate');
}
返回$new\u post\u id;
}
endif;
如果(!function_存在('reArrayFiles')):
函数reArrayFiles(&$file\u post){
$file_ary=array();
$file_count=count($file_post['name']);
$file\u keys=数组\u keys($file\u post);
对于($i=0;$i false));
if(isset($file_return['error'])| | isset($file_return['upload_error_handler'])){
返回false;
}否则{
$filename=$file_return['file'];
$attachment=array(
'post_mime_type'=>$file_return['type'],
'发布内容'=>'',
“post_类型”=>“附件”,
“post_status”=>“inherit”,
'guid'=>$file\u返回['url']
);
如果($标题){
$attachment['post_title']=$title;
}
$attachment\u id=wp\u insert\u attachment($attachment,$filename);
require_once(ABSPATH.'wp admin/includes/image.php');
$attachment\u data=wp\u generate\u attachment\u元数据($attachment\u id,$filename);
wp_更新_附件_元数据($attachment_id,$attachment_data);
如果(0if ( ! function_exists( 'post_imgs_update' ) ) :
        function post_imgs_update($new_post_id) {

            $data = array();
            $attachment_ids = array();
            // code for save featured image
            if( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'image_upload' ) ){  // here nonce text is "image_upload" you need to add in form
                    $files = reArrayFiles($_FILES['files']);

                if(!empty($_FILES['files'])){
                    //$i = 0; 
                    $set_featured_img = 0 ;

                    foreach( $files as $file ){
                        if( is_array($file) ){
                            $attachment_id = upload_user_file( $file, basename($file['name']) );
                            if ( is_numeric($attachment_id) ) {
                                if ($set_featured_img == '0') {
                                    set_post_thumbnail($new_post_id, $attachment_id);
                                    $set_featured_img++;
                                    $attachment_ids[] = $attachment_id;
                                }
                                else{
                                    $attachment_ids[] = $attachment_id;
                                }

                            }
                        }
                    }
                    //add to array
                    $mediaurl = array();
                    foreach ($attachment_ids as $img_id) {
                        $imgurl = wp_get_attachment_url( $img_id );
                        $image_gallery[$img_id] = $imgurl;
                    }

                    update_post_meta( $new_post_id, '_stw_property_multi_images', $image_gallery);      // 2nd arg field key
                }

            } 
            else {
                    $data['status'] = false;
                    $data['message'] = __('Nonce verify failed','realestate');
            }

            return $new_post_id;

        }
    endif;

    if ( ! function_exists( 'reArrayFiles' ) ) :
        function reArrayFiles(&$file_post) {
            $file_ary = array();
            $file_count = count($file_post['name']);
            $file_keys = array_keys($file_post);
            for ($i=0; $i<$file_count; $i++) {
                foreach ($file_keys as $key) {
                    $file_ary[$i][$key] = $file_post[$key][$i];
                }
            }
            return $file_ary;
        }
    endif;

    if ( ! function_exists( 'upload_user_file' ) ) :
        function upload_user_file( $file = array(), $title = false ) {
            require_once ABSPATH.'wp-admin/includes/admin.php';
            $file_return = wp_handle_upload($file, array('test_form' => false));
            if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){
                return false;
            }else{
                $filename = $file_return['file'];
                $attachment = array(
                    'post_mime_type' => $file_return['type'],
                    'post_content' => '',
                    'post_type' => 'attachment',
                    'post_status' => 'inherit',
                    'guid' => $file_return['url']
                );
                if($title){
                    $attachment['post_title'] = $title;
                }
                $attachment_id = wp_insert_attachment( $attachment, $filename );
                require_once(ABSPATH . 'wp-admin/includes/image.php');

                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
                wp_update_attachment_metadata( $attachment_id, $attachment_data );
                if( 0 < intval( $attachment_id ) ) {
                    return $attachment_id;
                }
            }
            return false;
        }
    endif;


    function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
        $attachurlarray = array();
        // check to make sure its a successful upload
        if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

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

        $attach_id = media_handle_upload( $file_handler, $post_id );
        return $attach_id;  
    }