Tuesday, September 26, 2017

Wordpress frontend submit form doesn't save selected categories and post description post_meta

Leave a Comment

I have one week working with a new Wordpress tube theme and I have created frontend submit form for the users with some limitation options in redux framework.This is first time working with file uploader for me.

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

When I submit the button many things working fine.Its saving the video file to the post, so title and video is okay and everything works fine except video categories and tags.

I have only 2 issues.

  1. I need to get the video description as a custom field.
  2. Video Categories are not saving.

2 Answers

Answers 1

For video descrption in post meta :

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

Important note :

For post meta : Remember if you are not having the add_meta function for this it will be not visible in your admin section but you can check the value in your database in postmeta table.

For tag : It is essential to have array in tags. I have put the comment above the code.

For categories : It is essential to have array in categories. I have put the comment above the code.

For Post content : post content cannot be null other wise it will stop creating your post.

Read my each and every comment mentioned in the code.

Answers 2

the problem was in categories name attribute because it requires array so I changed name="category" to name="category[]" and it worked fine I changed tag to post_tag and now its saving both tags and categories.

the problem on the video description was the same, a mistake on the name attribute now everything is working fine.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment