【问题标题】:Wordpress frontend submit form doesn't save selected categories and post description post_metaWordpress 前端提交表单不保存所选类别和帖子描述 post_meta
【发布时间】:2017-09-17 07:46:40
【问题描述】:

我有一周的时间使用新的 Wordpress 管主题,我为用户创建了前端提交表单,在 redux 框架中有一些限制选项。这是我第一次使用文件上传器。

<?php
if( !defined('ABSPATH') ) exit;
if( !class_exists('Tubemobile_ShortcodeSubmitVideo') ){
    class Tubemobile_ShortcodeSubmitVideo {
        function __construct() {
            add_action('init', array($this,'add_shortcodes'));
            add_action('wp_ajax_awpt_submit_video', array($this,'action_form'));
            add_action('wp_ajax_nopriv_awpt_submit_video', array($this,'action_form'));
        }
        function add_shortcodes(){
            add_shortcode('upload', array($this,'videotube_upload'));
        }
        function videotube_upload( $attr, $content = null){
            global $awpt;
            global $post;
            $html = null;
            extract(shortcode_atts(array(
                'vcategory' =>  'on',
                'vtag'      =>  'on',
                'cat_exclude'   =>  '',
                'cat_include'   =>  '',
                'cat_orderby'       =>  'name',
                'cat_order' =>  'DESC'
            ), $attr));
            $video_type = isset( $awpt['video-type'] ) ? $awpt['video-type'] : null;
            if( !is_array( $video_type ) ){
                $video_type = (array)$video_type;
            }
            $submit_roles = isset( $awpt['submit_roles'] ) ? (array)$awpt['submit_roles'] : 'author';
            if( count( $submit_roles ) == 1 ){
                $submit_roles = (array)$submit_roles;
            }
            //print_r($submit_roles);
            ### 0 is not allow guest, 1 is only register.
            $submit_permission = isset( $awpt['submit_permission'] ) ? $awpt['submit_permission'] : 0;
            $user_id = get_current_user_id();
            $current_user_role = awpt_get_user_role( $user_id );
            ### Check if Admin does not allow Visitor submit the video.
            if( $submit_permission == 0 && !$user_id ){
                $html .= '<a class="formLink" _form="login" href="/login" id="show_login"><i class="icon"><i class="fa fa-sign-in" aria-hidden="true"></i></i> <span>Login</span></a>';

                //$html .= '[login]';
            }
            //elseif( $submit_permission == 0 && !in_array( $current_user_role, $submit_roles) && $current_user_role != 'administrator'){
            elseif( $submit_permission == 0 && !in_array( $current_user_role, $submit_roles)){
                $html .= '
                    <div class="notifications "><div data-ntime="0" class="notifications__item notifications__item-error">'.__('You don\'t have the right permission to access this feature.','tubemobile').'</div></div>
                ';
            }
            else{
                $categories_html = null;
                $category_array = array(
                    'hide_empty'=>0,
                    'order' =>  $cat_order,
                    'orderby'   =>  $cat_orderby,
                );
                if( !empty( $cat_exclude ) ){
                    $cat_exclude = explode(",", $cat_exclude);
                    if( is_array( $cat_exclude ) ){
                        $category_array['exclude']  = $cat_exclude;
                    }
                }
                if( !empty( $cat_include ) ){
                    $cat_include = explode(",", $cat_include);
                    if( is_array( $cat_include ) ){
                        $category_array['include']  = $cat_include;
                    }
                }

                $categories = get_terms('category', $category_array);
                 if ( !empty( $categories ) && !is_wp_error( $categories ) ){
                    $categories_html .= '<select id="pre-selected-options" multiple="multiple">';
                    foreach ( $categories as $category ){
                        $categories_html .= '<option value="'.$category->term_id.'">'.$category->name.'</option>';
                    }
                    $categories_html .= '</select>';
                 }
                $html .= '
                <div class="holder" style="margin-top:0;">
                    <form role="form" action="" method="post" id="awpt-submit-video-form" enctype="multipart/form-data" style="padding:0;">
                      <div class="field_form post_title">
                        <label for="post_title">'.__('Video Title','tubemobile').'</label>
                        <span class="label label-danger">'.__('*','tubemobile').'</span>
                        <input type="text" class="input form-control" name="post_title" id="post_title">
                        <span class="help-block"></span>
                      </div>
                      <div class="field_form post_content">
                        <label for="post_content">'.__('Video Description','tubemobile').'</label>
                        ';
                        if( $awpt['submit_editor'] == 1 ){
                            $html .= awpt_get_editor('', 'post_content', 'post_content');
                        }
                        else{
                            $html .= '<div class="textarea_comment"><textarea name="post_content" id="post_content" class="textarea form-control" rows="3"></textarea></div>';
                        }
                      $html .= '<span class="help-block"></span>';
                      $html .= '</div>
                      <div class="field_form video-types">
                        <label for="post_title">'.__('Video Type','tubemobile').'</label>
                        <span class="label label-danger">'.__('*','tubemobile').'</span>';
                        if( in_array( 'videolink', $video_type ) ){
                            $html .= '
                                <div class="radio">
                                    <input type="radio" value="video_link_type" name="chb_video_type">'.__('Link','tubemobile').'
                                </div>
                            ';
                        }
                        if( in_array( 'embedcode', $video_type ) ){
                            $html .= '
                                <div class="radio">
                                    <input type="radio" value="embed_code_type" name="chb_video_type">'.__('Embed Code','tubemobile').'
                                </div>
                            ';
                        }
                        if( in_array( 'videofile', $video_type ) ){
                            $html .= '
                                <div class="radio">
                                    <input checked type="radio" value="file_type" name="chb_video_type">'.__('Upload file','tubemobile').'
                                </div>
                            ';
                        }
                      $html .= '
                      </div>';
                      if( in_array( 'videolink', $video_type ) ){
                        $html .= '
                          <div class="field_form video_url video-type video_link_type">
                            <label for="video_url">'.__('Video Link','tubemobile').'</label>
                            <span class="label label-danger">'.__('*','tubemobile').'</span>
                            <input type="text" class="input form-control" name="video_url" id="video_url" placeholder="Example: http://www.youtube.com/watch?v=X6pQ-pNSnRE">
                            <span class="help-block"></span>
                          </div>
                        ';
                      }
                      if( in_array( 'embedcode', $video_type ) ){
                        $html .= '
                          <div class="textarea_comment field_form embed_code_type video-type embed_code_type" style="display:none;">
                            <label for="video_url">'.__('Embed Code','tubemobile').'</label>
                            <span class="label label-danger">'.__('*','tubemobile').'</span>
                            <textarea class="form-control" name="embed_code" id="embed_code"></textarea>
                            <span class="help-block"></span>
                          </div>
                        ';
                      }
                      if( in_array( 'videofile', $video_type ) ){
                        $html .= '
                          <div class="field_form video_file video-type file_type" style="display:none;">
                            <label for="video_url">'.__('Video File','tubemobile').'</label>
                            <span class="label label-danger">'.__('*','tubemobile').'</span>
                            <input type="file" type="text" class="form-control" name="video_file" id="video_file">
                            <span class="help-block"></span>
                          </div>
                        ';
                      }
                      $html .= '
                      <div class="field_form video_thumbnail">
                        <label for="video_url">'.__('Video Preview Image','tubemobile').'</label>
                        <span class="label label-info">'.__('This image is required if you submit an embed code or a video file.','tubemobile').'</span>
                        <input type="file" type="text" class="form-control" name="video_thumbnail" id="video_thumbnail">
                        <span class="help-block"></span>

                      </div>';
                      if( $vtag == 'on' ):
                          $html .= '<div class="field_form video-tag">
                            <label for="key">'.__('Video Tag','tubemobile').'</label>
                            <input type="text" class="input form-control" name="post_tag" id="post_tag">
                          </div>';
                      endif;
                      if( $vcategory == 'on' ):
                        $html .= '<div class="field_form categories-video">
                            <label for="category">'.__('Category','tubemobile').'</label>';
                            $html .= $categories_html;
                          $html .= '</div>';
                      endif;
                      $html .= '<div class="btn_block"><input type="submit" class="input btn-primary""></div>
                      <img id="loading" style="display:none;">
                      <input type="hidden" name="current_page" value="'.$post->ID.'">
                      <input type="hidden" name="action" value="awpt_submit_video">
                      '.wp_nonce_field('submit_video_act','submit_video',true,false).'

                    </form></div>
                ';

            }
            return do_shortcode( $html );
        }
        function action_form(){
            global $awpt;
            $videosize = isset( $awpt['videosize'] ) ? (int)$awpt['videosize'] : 10;
            $post_title = wp_filter_nohtml_kses( $_POST['post_title'] );
            $video_url = isset( $_POST['video_url'] ) ? trim( $_POST['video_url'] ) : null;
            $embed_code = isset( $_POST['embed_code'] ) ? trim( $_POST['embed_code'] ) : null;
            $video_file = isset( $_FILES['video_file'] ) ? $_FILES['video_file'] : null;
            $post_content = wp_filter_nohtml_kses( $_POST['video_description'] );
            //$post_content = isset( $_POST['awpt_desc'] ) ? trim( $_POST['awpt_desc'] ) : null;
            $chb_video_type = isset( $_POST['chb_video_type'] ) ? $_POST['chb_video_type'] : null;
            $video_thumbnail = isset( $_FILES['video_thumbnail'] ) ? $_FILES['video_thumbnail'] : null;
            $post_tag = isset( $_POST['post_tag'] ) ? wp_filter_nohtml_kses( $_POST['post_tag'] ) : null;
            $video_category = isset( $_POST['video_category'] ) ? $_POST['video_category'] : null;
            $user_id = get_current_user_id() ? get_current_user_id() : $awpt['submit_assigned_user'];
            $post_status = $awpt['submit_status'] ? $awpt['submit_status'] : 'pending';
            $layout = isset( $_POST['layout'] ) ? $_POST['layout'] : 'small';


            if( !$post_title ){
                echo json_encode(array(
                    'resp'  =>  'error',
                    'message'   =>  __('Video Title is required','tubemobile'),
                    'element_id'    =>  'post_title'
                ));exit;
            }
            /*
            if( !$post_content ){
                echo json_encode(array(
                    'resp'  =>  'error',
                    'message'   =>  __('Video Description is required','tubemobile'),
                    'element_id'    =>  'post_content'
                ));exit;
            }
        */
            if( !$chb_video_type ){
                echo json_encode(array(
                    'resp'  =>  'error',
                    'message'   =>  __('Video Type is required','tubemobile'),
                    'element_id'    =>  'chb_video_type'
                ));exit;
            }

            switch ($chb_video_type) {
                case 'video_link_type':
                    if( !$video_url ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('Video Link is required','tubemobile'),
                            'element_id'    =>  'video_url'
                        ));exit;
                    }
                    if( !wp_oembed_get( $video_url ) ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('The link does not support.','tubemobile'),
                            'element_id'    =>  'video_url'
                        ));exit;
                    }
                break;

                case 'embed_code_type':
                    if( !$embed_code ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('Embed Code is required','tubemobile'),
                            'element_id'    =>  'embed_code'
                        ));exit;
                    }
                    if( apply_filters( 'awpt_submitform_thumbnail_required' , true) === true ):
                        if( !$video_thumbnail ){
                            echo json_encode(array(
                                'resp'  =>  'error',
                                'message'   =>  __('Video Preview Image is required','tubemobile'),
                                'element_id'    =>  'video_thumbnail'
                            ));exit;
                        }
                        if( !awpt_check_file_allowed( $video_thumbnail, 'image' ) ){
                            echo json_encode(array(
                                'resp'  =>  'error',
                                'message'   =>  __('Video Preview Image type is invalid','tubemobile'),
                                'element_id'    =>  'video_thumbnail'
                            ));exit;
                        }
                    endif;
                break;
                default:
                    if( !$video_file ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('Video File is required.','tubemobile'),
                            'element_id'    =>  'video_file'
                        ));exit;
                    }
                    if( !awpt_check_file_allowed( $video_file, 'video' ) ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('Video File format is invalid.','tubemobile'),
                            'element_id'    =>  'video_file'
                        ));exit;
                    }
                    if( !awpt_check_file_size_allowed($video_file) ){
                        echo json_encode(array(
                            'resp'  =>  'error',
                            'message'   =>  __('The video size must be less than ' . $videosize . 'MB','tubemobile'),
                            'element_id'    =>  'video_file'
                        ));exit;
                    }
                    if( apply_filters( 'awpt_submitform_thumbnail_required' , true) === true ):
                        if( !$video_thumbnail ){
                            echo json_encode(array(
                                'resp'  =>  'error',
                                'message'   =>  __('Video Preview Image is required','tubemobile'),
                                'element_id'    =>  'video_thumbnail'
                            ));exit;
                        }
                        if( !awpt_check_file_allowed( $video_thumbnail, 'image' ) ){
                            echo json_encode(array(
                                'resp'  =>  'error',
                                'message'   =>  __('Video Preview Image type is invalid','tubemobile'),
                                'element_id'    =>  'video_thumbnail'
                            ));exit;
                        }
                    endif;
                break;
            }

            /**
             * Error handler
             * @since Videotube V2.2.7
             */
            $errors = new WP_Error();
            $errors =   apply_filters( 'do_ajax_submit_video_errors' , $errors, $_POST );

            if ( ! empty( $errors->errors ) ) {
                echo json_encode(array(
                    'resp'  =>  'error',
                    'message'   =>  $errors->get_error_message(),
                    'element_id'    =>  $errors->get_error_code()
                ));exit;
            }

            $postarr = array(
                'post_title'    =>  $post_title,
                'post_content'  =>  $post_content,
                'post_type' =>  'post',
                'post_author'   =>  $user_id,
                'post_status'   =>  $post_status,
                'comment_status'    =>  'open'
            );

            $postarr    =   apply_filters( 'awpt_submit_data_args' , $postarr );

            $post_id = wp_insert_post($postarr, true);

            if ( is_wp_error( $post_id ) ){
                echo json_encode(array(
                    'resp'  =>  'error',
                    'message'   =>  $post_id->get_error_message()
                ));exit;
            }

            ###  update meta
            if( $layout ){
                update_post_meta( $post_id , 'layout', $layout);
            }
            if( $video_url ){
                update_post_meta( $post_id , 'video_url', $video_url);
            }
            elseif ( $embed_code){
                update_post_meta( $post_id , 'video_url', $embed_code);
            }
            else{
                ### Upload files.
                if( function_exists('awpt_insert_attachment') ){
                    awpt_insert_attachment('video_file', $post_id, false, 'video_file');
                    update_post_meta( $post_id , 'video_type', 'files');
                }
            }
            ### Preview image
            if( $video_thumbnail ){
                ### Upload files.
                if( function_exists('awpt_insert_attachment') ){
                    awpt_insert_attachment('video_thumbnail', $post_id, true);
                }
            }
            ### update term
            if( $post_tag ){
                wp_set_post_terms($post_id, $post_tag,'tag',true);
            }
            if( $video_category ){
                wp_set_post_terms($post_id, $video_category,'category',true);
            }
            do_action('awpt_save_post',$post_id);
            if( $post_status != 'publish' ){
                $redirect_to = $awpt['submit_redirect_to'] ? get_permalink( $awpt['submit_redirect_to'] ) : NULL;
                if( empty( $redirect_to ) ){
                    echo json_encode(array(
                        'resp'  =>  'success',
                        'message'   =>  __('Congratulation, Your submit is waiting for approval.','tubemobile'),
                        'post_id'   =>  $post_id,
                    ));exit;
                }
                else{
                    echo json_encode(array(
                        'resp'  =>  'success',
                        'message'   =>  __('Congratulation, Your submit is waiting for approval.','tubemobile'),
                        'post_id'   =>  $post_id,
                        'redirect_to'   =>  $redirect_to
                    ));exit;
                }
            }
            else{
                echo json_encode(array(
                    'resp'  =>  'publish',
                    'message'   =>  __('Congratulation, Your submit is published.','tubemobile'),
                    'post_id'   =>  $post_id,
                    'redirect_to'   =>  get_permalink( $post_id )
                ));exit;
            }
        }
    }
    new Tubemobile_ShortcodeSubmitVideo();
}

当我提交按钮时,很多东西都可以正常工作。它将视频文件保存到帖子中,所以标题和视频没问题,除了视频类别和标签外一切正常。

我只有 2 个问题。

  1. 我需要将视频描述作为自定义字段。
  2. 视频类别未保存。

【问题讨论】:

标签: php wordpress file-upload


【解决方案1】:

对于帖子元中的视频描述:

 $postarr = array(
            'post_title'    =>  $post_title,
            'post_content'  =>  $post_title,// set any value here other wise it will stop inserting your post
            'post_type' =>  'post',
            'post_author'   =>  $user_id,
            'post_status'   =>  $post_status,
            'comment_status'    =>  'open'
        );

        $postarr    =   apply_filters( 'awpt_submit_data_args' , $postarr );

        $post_id = wp_insert_post($postarr, true);
        //this will update your post meta 
        update_post_meta( $post_id, 'videoDescrption', $post_content);
        //below line is write to give you the understanding that you need to pass it as a array
        $arrayoftags=$video_tag;
        wp_set_post_terms( $post_id, $arrayoftags);
        //below line is write to give you the understanding that you need to pass it as a array
        $arrayoftags = $video_category;
        wp_set_post_categories( $post_id, $arrayofcategories );

重要提示:

对于 post meta :请记住,如果您没有 add_meta 功能,它将在您的管理部分中不可见,但您可以在 postmeta 表中检查数据库中的值。

对于标签:标签中必须有数组。我已将注释放在代码上方。

对于类别:类别中必须有数组。我已将注释放在代码上方。

对于帖子内容:帖子内容不能为空,否则它将停止创建您的帖子。

阅读我在代码中提到的每一条评论。

【讨论】:

  • 感谢您的回复 我仔细阅读了您所看到的我在 $post_content = wp_filter_nohtml_kses( $_POST['video_description'] ); 下面更改了 $post_content但是我现在要测试它再次感谢
  • 因此,在 action_form() 中创建了一个新行,例如 $video_description = isset( $_POST['video_description'] ) ? trim( $_POST['video_description'] ) : null;,而不是在 postarr 我为 post_content 添加了一个值,例如 'post_content' =&gt; $video_description, 我还在 save_post 函数中添加了一个新行:if( $video_description ){ update_post_meta( $post_id , 'video_description', $video_description); } 否运气现在它不保存帖子并且提交按钮没有响应所以我看到加载图像并且不再重定向到成功页面。
  • 我还将 html 中的 name 属性从 post_content 更改为 video_description,但仍然没有发布任何内容,而是保留在实际页面上。
  • 检查您是否获得了 $video_description 中的值以及是否为 $post_id?如果您在 updatpost 元数据 update_post_meta( $post_id , 'video_description', $video_description,true) 中获得比通过 true ;
  • 但是,当我再次将 postarr 行更改为 $_post_content 并且另外对于视频描述时,我执行了与 embed_code 相同的步骤,它工作正常,但它仍然没有将帖子元更新到数据库中跨度>
【解决方案2】:

问题出在类别名称属性中,因为它需要数组,所以我将name="category" 更改为name="category[]",它工作正常我将标签更改为 post_tag,现在它同时保存了标签和类别。

视频描述的问题是一样的,名称属性的错误现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多