php - Wordpress frontend submit form doesn't save selected categories and post description post_meta -
i have 1 week working new wordpress tube theme , have created frontend submit form users limitation options in redux framework.this first time working file uploader 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 not allow guest, 1 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 not allow visitor submit 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 right permission access 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 $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 required if submit embed code or 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 required','tubemobile'), 'element_id' => 'post_title' ));exit; } /* if( !$post_content ){ echo json_encode(array( 'resp' => 'error', 'message' => __('video description required','tubemobile'), 'element_id' => 'post_content' ));exit; } */ if( !$chb_video_type ){ echo json_encode(array( 'resp' => 'error', 'message' => __('video type 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 required','tubemobile'), 'element_id' => 'video_url' ));exit; } if( !wp_oembed_get( $video_url ) ){ echo json_encode(array( 'resp' => 'error', 'message' => __('the link 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 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 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 invalid','tubemobile'), 'element_id' => 'video_thumbnail' ));exit; } endif; break; default: if( !$video_file ){ echo json_encode(array( 'resp' => 'error', 'message' => __('video file 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 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 less ' . $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 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 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, submit waiting approval.','tubemobile'), 'post_id' => $post_id, ));exit; } else{ echo json_encode(array( 'resp' => 'success', 'message' => __('congratulation, submit waiting approval.','tubemobile'), 'post_id' => $post_id, 'redirect_to' => $redirect_to ));exit; } } else{ echo json_encode(array( 'resp' => 'publish', 'message' => __('congratulation, submit published.','tubemobile'), 'post_id' => $post_id, 'redirect_to' => get_permalink( $post_id ) ));exit; } } } new tubemobile_shortcodesubmitvideo(); }
when submit button many things working fine.its saving video file post, title , video okay , works fine except video categories , tags.
i have 2 issues.
- i need video description custom field.
- video categories not saving.
for video descrption in post meta :
$postarr = array( 'post_title' => $post_title, 'post_content' => $post_title,// set value here other wise stop inserting 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 update post meta update_post_meta( $post_id, 'videodescrption', $post_content); //below line write give understanding need pass array $arrayoftags=$video_tag; wp_set_post_terms( $post_id, $arrayoftags); //below line write give understanding need pass array $arrayoftags = $video_category; wp_set_post_categories( $post_id, $arrayofcategories );
important note :
for post meta : remember if not having add_meta function not visible in admin section can check value in database in postmeta table.
for tag : essential have array in tags. have put comment above code.
for categories : essential have array in categories. have put comment above code.
for post content : post content cannot null other wise stop creating post.
read each , every comment mentioned in code.
Comments
Post a Comment