2017-09-17 45 views
1

我有一個星期的工作與一個新的WordPress的管主題,我已經創建了用戶的前端提交表單,在redux框架中有一些限制選項。這是第一次與我的文件上傳工作。WordPress的前端提交表格不保存所選的類別和文章描述post_meta

<?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(); 
} 

當我提交按鈕很多事情工作fine.Its保存視頻文件的帖子,所以標題和視頻是好的,一切工作除了視頻類和標籤罰款。

我只有2個問題。

  1. 我需要將視頻描述作爲自定義字段。
  2. 視頻分類不存儲。
+0

你有什麼已經試圖做到這一點?請回顧[預計需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)。堆棧溢出不是一種編碼服務。您需要研究您的問題,並嘗試在發佈之前親自編寫代碼。如果你遇到* specific *,返回幷包含一個[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)以及你試過的內容摘要 – FluffyKitten

回答

0

問題是大類的名稱屬性,因爲它要求陣列,所以我改變name="category"name="category[]"它工作得很好,我改變標籤post_tag,現在它節省了標籤和分類。

視頻說明上的問題是一樣的,名稱屬性上的錯誤現在一切工作正常。

2

對於柱體元視頻descrption:

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

重要提示:

對於後期薈萃:請記住,如果你沒有這樣做的add_meta功能這將是不可見您的管理部分,但您可以在postmeta表中檢查數據庫中的值。

對於標籤:在標籤中使用數組是很重要的。我已將註釋放在代碼上面。

對於類別:必須具有數組的類別。我已將註釋放在代碼上面。

對於發佈內容:發佈內容不能爲空其他明智它將停止創建您的帖子。

閱讀我在代碼中提到的每一條評論。

+0

感謝你的回覆Im仔細閱讀,因爲你看到我改變$ post_content低於$ post_content = wp_filter_nohtml_kses($ _POST ['video_description']);但是我現在要再次測試它,再次感謝 – Gazi

+0

因此,在action_form()中創建了一個新行,比如'$ video_description = isset($ _POST ['video_description'])?修剪($ _POST [ 'VIDEO_DESCRIPTION']):空;'比postarr我添加了一個值到POST_CONTENT像' 'POST_CONTENT' \t => \t $ VIDEO_DESCRIPTION,'也我加入save_post功能的新行:\t \t \t 'if($ video_description){ \t \t \t \t update_post_meta($ post_id,'video_description',$ video_description); \t \t \t}'現在沒有運氣,它不保存文章,提交按鈕不響應,所以我看到加載圖像,沒有更多的重定向到成功頁面。 – Gazi

+0

我也將html中的名稱屬性從post_content更改爲video_description,但仍然不會發布它保留在實際頁面上的任何內容。 – Gazi

相關問題