Wordpress - Custom post image on frontend doesn't upload - php

I've one form to upload an attachment file
<form id="post-job-form" class="frontend-form" action="" method="post" enctype="multipart/form-data" role="form">
<?php
$status_message = '';
if( isset( $_GET['message'] ) ){
$status_message = $_GET['message'];
}
// check if user have post a company
$check = get_posts( array( 'post_type' => 'company', 'author' => get_current_user_id() ) );
if( $check == null ){
$status_message = '6';
}//endif;
jobboard_set_post_message( $status_message );
?>
<div class="form-group">
<label for="sallary_periode"><?php _e( 'Salaire Periode', 'jobboard' ); ?></label>
<select name="sallary_periode" id="sallary_periode" class="form-control">
<?php
$sallary_periode = array(
'hourly' => 'Hourly',
'daily' => 'Daily',
'weekly' => 'Weekly',
'monthly' => 'Monthly'
);
foreach( $sallary_periode as $periode_key => $periode_value ) {
$selected = '';
if( $default['sallary_periode'] == $periode_key ){
$selected = 'selected';
}
echo '<option value="'.$periode_key.'" '.$selected.'>'.esc_attr($periode_value).'</option>';
}
?>
</select>
</div><!-- /.form-group -->
<div class="form-group">
<label for="job_img"><?php _e( 'L`image (en option)', 'jobboard' ); ?></label>
<?php
if( isset($_GET['action']) && $_GET['action'] == 'edit' ){
$edit = get_post( $_GET['jid'] );
$jobimag = get_post_meta( $edit->ID, 'jbchild_meta_job_image', true );
if(!empty($jobimag)){
echo '<img src="' . $jobimag . '">';
}//!empty
}//endif;
?>
<input class="" type="file" name="job_photo" id="job_photo" accept="image/*" />
<span class="help-block"><?php _e( 'Télécharger éventuellement votre image pour les candidats pour voir', 'jobboard' ); ?></span>
</div><!-- /.form-group -->
<?php
if( isset( $_GET['action'] ) && $_GET['action'] == 'edit' ){
$button_text = __( 'Update Job', 'jobboard' );
?>
<input type="hidden" name="form_type" id="form_type" value="edit_post_job" />
<input type="hidden" name="post_id" id="post_id" value="<?php echo esc_attr( $default['post_id'] ); ?>" />
<?php
}else{
$button_text = __( 'Post A Job', 'jobboard' );
?>
<input type="hidden" name="form_type" id="form_type" value="post_job" />
<?php
}
?>
<input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( get_current_user_id() ); ?>" />
<button type="submit" name="submit" class="btn btn-post-resume"><?php echo esc_attr( $button_text ); ?></button>
</form>
And this is my function:
function jobboard_post_job_123( $data = array(), $files = array(), $update = false ){
$post_status = 'publish';
if(jobboard_option('enable_highlight_package_job') == '1'){
$post_status ='publish';
}
$job_args = array(
'post_content' => $data['job_description'],
'post_title' => $data['job_title'],
'post_status' => $post_status,
'post_type' => 'job',
'post_author' => $data['user_id'],
);
$message = '1';
if( $update ){
$job_args['ID'] = $data['post_id'];
$job_args['post_status'] = get_post_status( $data['post_id'] );
$message = '2';
}
$job_id = wp_insert_post( $job_args );
if($job_id){
if( isset( $data['job_region'] ) ){
wp_set_object_terms( $job_id, $data['job_region'], 'job_region' );
}
if( isset( $data['job_type'] ) ){
wp_set_object_terms( $job_id, $data['job_type'], 'job_type' );
}
if( isset( $data['job_category'] ) ){
wp_set_object_terms( $job_id, $data['job_category'], 'job_category' );
}
// Job Company Metabox
update_post_meta( $job_id, '_jboard_job_company', $data['job_company'] );
// Job Experience Metabox
update_post_meta( $job_id, '_jboard_job_experiences', $data['job_experience'] );
// Job Salary Metabox
update_post_meta( $job_id, '_jboard_job_sallary', $data['job_sallary'] );
// Job Salary Periode
update_post_meta( $job_id, '_jboard_sal_periode', $data['sallary_periode'] );
// Job Summary Metabox
update_post_meta( $job_id, '_jboard_job_summary', $data['job_summary'] );
// Job Overview Metabox
update_post_meta( $job_id, '_jboard_job_overview', $data['job_overview'] );
if( !empty( $files['job_photo']['name'] ) ){
$job_img = jobboard_file_upload( $files['job_photo'], 'file' );
$old_job_img = get_post_meta( $job_id, 'jbchild_meta_job_image', true );
if($job_img){
//update_post_meta( $job_id, 'jbchild_meta_job_image', $job_img['url'], $old_job_img );
update_post_meta( $job_id, 'jbchild_meta_job_image', 'http://www.soslivreur.fr/wp-content/uploads/2016/04/20160316_174725.jpg' );
}
}
// Job metabox data set
$job_meta = array(
'_jboard_sal_periode'
);
update_post_meta( $job_id, 'jobboard_job_mb_fields', $job_meta );
wp_redirect( esc_url(add_query_arg( array( 'action' => 'edit', 'jid' => $job_id, 'message' => $message ) ) ) );
exit;
}
}
I have a problem. When I'm submitting this form others field can insert to database but an image can't insert to database.
How to fix my code? Thank you

please replace this line of code in your function file function name is jobboard_post_job_123
if( !empty( $files['job_photo']['name'] ) ){
$job_img = jobboard_file_upload( $files['job_photo'], 'file' );
//$old_job_img = get_post_meta( $job_id, 'jbchild_meta_job_image', true );
if($job_img != ''){
update_post_meta( $job_id, 'jbchild_meta_job_image', 'http://www.soslivreur.fr/wp-content/uploads/2016/04/20160316_174725.jpg' );
}
}
And then check it's work or not becuase you had predefine Old job image on that.

Related

PHP/Dokan/WooCommerce. Change input type="text" to input="checkbox"

We have a WordPress site using Dokan.
I'd like to add a checkbox where the user can check whether the product should be a donation.
I got this snippet that I'm trying to modify. My PHP is a bit too basic. Please help.
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="text" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Product Code:', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}
This is how far I got:
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="checkbox" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Donation Product', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}
The rest I'm struggling with. Many thanks for any help on this.

Duplicate div in php

I created this PHP code in WordPress to add image to the menu, but it is duplicating a div where there is an add button and I don't know why, I need it to be only one and not two results of the div, in case I only want the left div... I'll add an image about what I'm talking about so you can analyze my problem!
Structure is placed inside function wp_post_thumbnail_only_html
I left only the action that passes the button to facilitate.
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
class SNAP_Menu_Image{
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ), 99 );
}
public function admin_init() {
if ( !has_action( 'wp_nav_menu_item_custom_fields' ) ) {
}
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'menu_item_custom_fields' ), 10, 4 );
}
public function file_is_displayable_image( $result, $path ) {
if ( $result ) {
return true;
}
$fileExtension = pathinfo( $path, PATHINFO_EXTENSION );
return in_array( $fileExtension, $this->additionalDisplayableImageExtensions );
}
public function menu_image_init() {
add_post_type_support( 'nav_menu_item', array( 'thumbnail' ) );
$this->image_sizes = apply_filters( 'menu_image_default_sizes', $this->image_sizes );
if (is_array($this->image_sizes)) {
foreach ($this->image_sizes as $name => $params) {
add_image_size($name, $params[0], $params[1], ( array_key_exists(2, $params) ? $params[2] : false ) );
}
}
load_plugin_textdomain( 'menu-image', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
public function menu_image_nav_menu_manage_columns( $columns ) {
return $columns + array( 'image' => __( 'Image', 'menu-image' ) );
}
public function menu_image_save_post_action( $post_id, $post ) {
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position'
);
foreach ( $menu_image_settings as $setting_name ) {
if ( isset( $_POST[ $setting_name ][ $post_id ] ) && !empty( $_POST[ $setting_name ][ $post_id ] ) ) {
if ( $post->{"_$setting_name"} != $_POST[ $setting_name ][ $post_id ] ) {
update_post_meta( $post_id, "_$setting_name", esc_sql( $_POST[ $setting_name ][ $post_id ] ) );
}
}
}
}
public function wp_update_nav_menu_item_action( $item_menu_id, $menu_item_db_id ) {
global $sitepress, $icl_menus_sync;
if ( class_exists( 'SitePress' ) && $sitepress instanceof SitePress && class_exists( 'ICLMenusSync' ) && $icl_menus_sync instanceof ICLMenusSync ) {
static $run_times = array();
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position',
'thumbnail_id',
'thumbnail_hover_id',
);
foreach ( $icl_menus_sync->menus as $menu_id => $menu_data ) {
if ( !isset( $_POST[ 'sync' ][ 'add' ][ $menu_id ] ) ) {
continue;
}
$cache_key = md5( serialize( array( $item_menu_id, 'tax_nav_menu' ) ) );
$cache_group = 'get_language_for_element';
wp_cache_delete( $cache_key, $cache_group );
$lang = $sitepress->get_language_for_element( $item_menu_id, 'tax_nav_menu' );
if ( !isset( $run_times[ $menu_id ][ $lang ] ) ) {
$run_times[ $menu_id ][ $lang ] = 0;
}
$post_item_ids = array();
foreach ( $_POST[ 'sync' ][ 'add' ][ $menu_id ] as $id => $lang_array ) {
if ( array_key_exists( $lang, $lang_array ) ) {
$post_item_ids[ ] = $id;
}
}
if ( !array_key_exists( $run_times[ $menu_id ][ $lang ], $post_item_ids ) ) {
continue;
}
$orig_item_id = $post_item_ids[ $run_times[ $menu_id ][ $lang ] ];
$orig_item_meta = get_metadata( 'post', $orig_item_id );
foreach ( $menu_image_settings as $meta ) {
if ( isset( $orig_item_meta[ "_$meta" ] ) && isset( $orig_item_meta[ "_$meta" ][ 0 ] ) ) {
update_post_meta( $menu_item_db_id, "_$meta", $orig_item_meta[ "_$meta" ][ 0 ] );
}
}
$run_times[ $menu_id ][ $lang ] ++;
break;
}
}
}
public function menu_image_edit_nav_menu_walker_filter() {
return 'Menu_Image_Walker_Nav_Menu_Edit';
}
public function menu_image_wp_setup_nav_menu_item( $item ) {
if ( !isset( $item->thumbnail_id ) ) {
$item->thumbnail_id = get_post_thumbnail_id( $item->ID );
}
if ( !isset( $item->thumbnail_hover_id ) ) {
$item->thumbnail_hover_id = get_post_meta( $item->ID, '_thumbnail_hover_id', true );
}
if ( !isset( $item->image_size ) ) {
$item->image_size = get_post_meta( $item->ID, '_menu_item_image_size', true );
}
if ( !isset( $item->title_position ) ) {
$item->title_position = get_post_meta( $item->ID, '_menu_item_image_title_position', true );
}
return $item;
}
public function menu_image_nav_menu_link_attributes_filter( $atts, $item, $args, $depth = null ) {
$this->setProcessed( $item->ID );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = ! empty( $atts[ 'class' ] ) ? $atts[ 'class' ] : '';
$class .= " menu-image-title-{$position}";
if ( $item->thumbnail_hover_id ) {
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$class .= ' menu-image-not-hovered';
}
if ( ! empty( $args->walker ) && class_exists( 'FlatsomeNavDropdown' ) && $args->walker instanceof FlatsomeNavDropdown && ! is_null( $depth ) && $depth === 0 ) {
$class .= ' nav-top-link';
}
$atts[ 'class' ] = trim( $class );
return $atts;
}
public function menu_image_nav_menu_item_title_filter( $title, $item, $depth = null, $args = null) {
if (is_numeric($item)) {
$item = wp_setup_nav_menu_item( get_post( $item ) );
}
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
}
$none = ''; // Sugar.
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $title, $image );
break;
case 'after':
default:
$item_args = array( $image, $title, $none );
break;
}
$title = vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
return $title;
}
public function menu_image_nav_menu_item_filter( $item_output, $item, $depth, $args ) {
if ( $this->isProcessed( $item->ID ) ) {
return $item_output;
}
$attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
$attributes .= !empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= !empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= !empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
$attributes_array = shortcode_parse_atts($attributes);
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$class .= ' menu-image-not-hovered';
}
$attributes_array['class'] = $class;
$attributes_array = apply_filters( 'menu_image_link_attributes', $attributes_array, $item, $depth, $args );
$attributes = '';
foreach ( $attributes_array as $attr_name => $attr_value ) {
$attributes .= "{$attr_name}=\"$attr_value\" ";
}
$attributes = trim($attributes);
$item_output = "{$args->before}<a {$attributes}>";
$link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$none = '';
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $link, $image );
break;
case 'after':
default:
$item_args = array( $image, $link, $none );
break;
}
$item_output .= vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
$item_output .= "</a>{$args->after}";
return $item_output;
}
public function menu_image_add_inline_style_action() {
wp_register_style( 'menu-image', SF()->plugin_url() . '/assets/css/menu-image.css', array(), '1.1', 'all' );
wp_enqueue_style( 'menu-image' );
}
public function menu_image_admin_head_nav_menus_action() {
wp_enqueue_script( 'menu-image-admin', SF()->plugin_url() . '/assets/js/menu-image.js', array( 'jquery' ) );
wp_localize_script(
'menu-image-admin', 'menuImage', array(
'l10n' => array(
'uploaderTitle' => __( 'Chose menu image', 'menu-image' ),
'uploaderButtonText' => __( 'Select', 'menu-image' ),
),
'settings' => array(
'nonce' => wp_create_nonce( 'update-menu-item' ),
),
)
);
wp_enqueue_media();
wp_enqueue_style( 'editor-buttons' );
}
public function menu_image_delete_menu_item_image_action() {
$menu_item_id = (int) $_REQUEST[ 'menu-item' ];
check_admin_referer( 'delete-menu_item_image_' . $menu_item_id );
if ( is_nav_menu_item( $menu_item_id ) && has_post_thumbnail( $menu_item_id ) ) {
delete_post_thumbnail( $menu_item_id );
delete_post_meta( $menu_item_id, '_thumbnail_hover_id' );
delete_post_meta( $menu_item_id, '_menu_item_image_size' );
delete_post_meta( $menu_item_id, '_menu_item_image_title_position' );
}
}
public function wp_post_thumbnail_only_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$markup = '<p class="description description-thin" ><label>%s<br /><a title="%s" href="#" class="set-post-thumbnail button%s" data-item-id="%s" style="height: auto;">%s</a>%s</label></p>';
$thumbnail_id = get_post_thumbnail_id( $item_id );
$content = sprintf(
$markup,
esc_html__( 'Menu image', 'menu-image' ),
$thumbnail_id ? esc_attr__( 'Change menu item image', 'menu-image' ) : esc_attr__( 'Set menu item image', 'menu-image' ),
'',
$item_id,
$thumbnail_id ? wp_get_attachment_image( $thumbnail_id, $default_size ) : esc_html__( 'Set image', 'menu-image' ),
$thumbnail_id ? '' . __( 'Remove', 'menu-image' ) . '' : ''
);
return $content;
}
public function wp_post_thumbnail_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$content = $this->wp_post_thumbnail_only_html( $item_id );
$image_size = get_post_meta( $item_id, '_menu_item_image_size', true );
if ( !$image_size ) {
$image_size = $default_size;
}
$title_position = get_post_meta( $item_id, '_menu_item_image_title_position', true );
if ( !$title_position ) {
$title_position = apply_filters( 'menu_image_default_title_position', 'after' );
}
$content = "<div class='menu-item-images' style='min-height:70px'>$content</div>";
return apply_filters( 'admin_menu_item_thumbnail_html', $content, $item_id );
}
public function wp_ajax_set_menu_item_thumbnail() {
$json = !empty( $_REQUEST[ 'json' ] );
$post_ID = intval( $_POST[ 'post_id' ] );
if ( !current_user_can( 'edit_post', $post_ID ) ) {
wp_die( - 1 );
}
$thumbnail_id = intval( $_POST[ 'thumbnail_id' ] );
$is_hovered = (bool) $_POST[ 'is_hover' ];
check_ajax_referer( "update-menu-item" );
if ( $thumbnail_id == '-1' ) {
if ( $is_hovered ) {
$success = delete_post_meta( $post_ID, '_thumbnail_hover_id' );
} else {
$success = delete_post_thumbnail( $post_ID );
}
} else {
if ( $is_hovered ) {
$success = update_post_meta( $post_ID, '_thumbnail_hover_id', $thumbnail_id );
} else {
$success = set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
if ( $success ) {
$return = $this->wp_post_thumbnail_only_html( $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
}
wp_die( 0 );
}
public function menu_item_custom_fields( $item_id, $item, $depth, $args ){
?>
<div class="field-image hide-if-no-js wp-media-buttons">
<?php echo $this->wp_post_thumbnail_html( $item_id ) ?>
</div>
<?php
}
public function jetpack_photon_override_image_downsize_filter( $prevent, $data ) {
return $this->isAttachmentUsed( $data[ 'attachment_id' ], $data[ 'size' ] );
}
public function setUsedAttachments( $size, $id ) {
$this->used_attachments[ $size ][ ] = $id;
}
public function isAttachmentUsed( $id, $size = null ) {
if ( ! is_null( $size ) ) {
return is_string( $size ) && isset( $this->used_attachments[ $size ] ) && in_array( $id, $this->used_attachments[ $size ] );
} else {
foreach ( $this->used_attachments as $used_attachment ) {
if ( in_array( $id, $used_attachment ) ) {
return true;
}
}
return false;
}
}
public function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
if ( $this->isAttachmentUsed( $attachment->ID, $size ) ) {
unset( $attr['sizes'], $attr['srcset'] );
}
return $attr;
}
protected function setProcessed( $id ) {
$this->processed[] = $id;
}
protected function isProcessed( $id ) {
return in_array( $id, $this->processed );
}
}
$menu_image = new SNAP_Menu_Image();
require_once(ABSPATH . 'wp-admin/includes/nav-menu.php');
class Menu_Image_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
ob_start();
$item_id = esc_attr( $item->ID );
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
$original_title = false;
if ( 'taxonomy' == $item->type ) {
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
if ( is_wp_error( $original_title ) )
$original_title = false;
} elseif ( 'post_type' == $item->type ) {
$original_object = get_post( $item->object_id );
$original_title = get_the_title( $original_object->ID );
} elseif ( 'post_type_archive' == $item->type ) {
$original_object = get_post_type_object( $item->object );
if ( $original_object ) {
$original_title = $original_object->labels->archives;
}
}
$classes = array(
'menu-item menu-item-depth-' . $depth,
'menu-item-' . esc_attr( $item->object ),
'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
);
$title = $item->title;
if ( ! empty( $item->_invalid ) ) {
$classes[] = 'menu-item-invalid';
$title = sprintf( __( '%s (Invalid)' ), $item->title );
} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
$classes[] = 'pending';
$title = sprintf( __('%s (Pending)'), $item->title );
}
$title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
$submenu_text = '';
if ( 0 == $depth )
$submenu_text = 'style="display: none;"';
?>
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
<div class="menu-item-bar">
<div class="menu-item-handle">
<span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
<span class="item-controls">
<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
<span class="item-order hide-if-js">
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-up-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-up" aria-label="<?php esc_attr_e( 'Move up' ) ?>">↑</a>
|
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-down-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-down" aria-label="<?php esc_attr_e( 'Move down' ) ?>">↓</a>
</span>
<a class="item-edit" id="edit-<?php echo $item_id; ?>" href="<?php
echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
?>" aria-label="<?php esc_attr_e( 'Edit menu item' ); ?>"><span class="screen-reader-text"><?php _e( 'Edit' ); ?></span></a>
</span>
</div>
</div>
<div class="menu-item-settings wp-clearfix" id="menu-item-settings-<?php echo $item_id; ?>">
<?php if ( 'custom' == $item->type ) : ?>
<p class="field-url description description-wide">
<label for="edit-menu-item-url-<?php echo $item_id; ?>">
<?php _e( 'URL' ); ?><br />
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
</label>
</p>
<?php endif; ?>
<p class="field-xfn description description-thin">
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
<?php _e( 'Link Relationship (XFN)' ); ?><br />
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
</label>
</p>
<?php
do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
?>
<fieldset class="field-move hide-if-no-js description description-wide">
<span class="field-move-visual-label" aria-hidden="true"><?php _e( 'Move' ); ?></span>
<button type="button" class="button-link menus-move menus-move-up" data-dir="up"><?php _e( 'Up one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-down" data-dir="down"><?php _e( 'Down one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-left" data-dir="left"></button>
<button type="button" class="button-link menus-move menus-move-right" data-dir="right"></button>
<button type="button" class="button-link menus-move menus-move-top" data-dir="top"><?php _e( 'To the top' ); ?></button>
</fieldset>
<div class="menu-item-actions description-wide submitbox">
<?php if ( 'custom' != $item->type && $original_title !== false ) : ?>
<p class="link-to-original">
<?php printf( __('Original: %s'), '' . esc_html( $original_title ) . '' ); ?>
</p>
<?php endif; ?>
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'delete-menu-item',
'menu-item' => $item_id,
),
admin_url( 'nav-menus.php' )
),
'delete-menu_item_' . $item_id
); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
</div>
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
</div>
<ul class="menu-item-transport"></ul>
<?php
$output .= ob_get_clean();
}
}
As basic as the answer is, I'll leave the question active...
I had instantiated the class 2 times!

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

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.
I need to get the video description as a custom field.
Video Categories are not saving.
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.
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.

How to filter get previous post function by meta value DESC and post date DESC?

My loop displays featured posts and then posts in reverse chronological order.
However when I use <?php echo get_previous_post(); ?> it grabs posts in reverse chronological order.
How do I add a filter to the previous post function similiar to this but instead filter by an array like this: 'orderby' => array( 'meta_value' =>
'DESC', 'date' => 'DESC') ?
Code:
UPDATE: Cleaned up index.php loop after help from #sMyles and #JackJohansson
Index.php loop:
$args = array(
'posts_per_page' => - 1,
'meta_key' => 'meta-checkbox',
'orderby' => array( 'meta_value' => 'DESC', 'date' => 'DESC')
);
$posts = new WP_Query( $args );
if($posts->have_posts() ){
while( $posts->have_posts() ){
$posts->the_post();
// Set to content template by default
$template = 'content';
// Change template to featured when `is_featured` meta has a value
if(get_post_meta(get_the_ID(), 'meta-checkbox', 'yes')){
$template = 'featured';
}
// Load template part
get_template_part( $template, get_post_format() );
}
}
UPDATE: As suggested, I added where I use the previous post function
Previous Post Function
<div id="next-post">
<?php $prev_post = get_previous_post();
if(!empty($prev_post)) {
echo '<a class="next-story" href="' . get_permalink($prev_post->ID) . '">Next Story</a>';
echo '<a class="next-story-title" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>';
} ?>
</div>
Function For Featured Posts:
function sm_custom_meta() {
add_meta_box( 'sm_meta', __( 'Featured Posts', 'sm-textdomain' ), 'sm_meta_callback', 'post' );
}
function sm_meta_callback( $post ) {
$featured = get_post_meta( $post->ID );
?>
<p>
<div class="sm-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured['meta-checkbox'] ) ) checked( $featured['meta-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Featured this post', 'sm-textdomain' )?>
</label>
</div>
</p>
<?php
}
add_action( 'add_meta_boxes', 'sm_custom_meta' );
function sm_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'sm_nonce' ] ) && wp_verify_nonce( $_POST[ 'sm_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox' ] ) ) {
update_post_meta( $post_id, 'meta-checkbox', 'yes' );
} else {
update_post_meta( $post_id, 'meta-checkbox', '' );
}
}
add_action( 'save_post', 'sm_meta_save' );
Try the following args
$args = array(
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes',
'posts_per_page' => -1,
'order_by'=> 'meta_value',
'order'=>'ASC'
);

Wordpress Meta Box Form Data Not Saving

My plugin is about adding FAQs to your desired page.
I have added a form within a metabox in the 'Add New Question'.
When I feed values in the form, the form data does save in
the database, but does not retain or show the values (selected)
in the form when the page updates/publishes.
Instead, the form shows the previous non-selected values (even though the
selected values do show up in the database).
<?php
function faq_manage_things($post)
{
global $post;
$post_id= $post->ID;
wp_nonce_field(basename(__FILE__),'faqs_questions_nonce');
$faq_stored_meta= get_post_meta($post->ID);
?>
<div class="wrap">
<p>
<label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label>
<select name="meta-select" id="meta-select">
<option value="select-one" <?php if ( isset ( $prfx_stored_meta['meta-select'] ) ) selected( $prfx_stored_meta['meta-select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>';
<option value="select-two" <?php if ( isset ( $prfx_stored_meta['meta-select'] ) ) selected( $prfx_stored_meta['meta-select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>';
</select>
</p>
<!-- TEXT AREA -->
<p>
<label for="meta-textarea" class="prfx-row-title"><?php _e( 'Example Textarea Input', 'prfx-textdomain' )?></label>
<textarea name="meta-textarea" id="meta-textarea"><?php if ( isset ( $prfx_stored_meta['meta-textarea'] ) ) echo $prfx_stored_meta['meta-textarea'][0]; ?></textarea>
</p>
<!-- CHECKBOXES -->
<p>
<span class="prfx-row-title"><?php _e( 'Example Checkbox Input', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox'] ) ) checked( $prfx_stored_meta['meta-checkbox'][0], 'yes' ); ?> /><?php _e( 'Checkbox label', 'prfx-textdomain' )?>
</label>
<label for="meta-checkbox-two">
<input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox-two'] ) ) checked( $prfx_stored_meta['meta-checkbox-two'][0], 'yes' ); ?> /><?php _e( 'Another checkbox', 'prfx-textdomain' )?>
</label>
</div>
</p>
</div>
<?php
}
global $post;
function faq_meta_save( $post_id ) {
global $post;
$post_id= $post->ID;
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'faqs_questions_nonce' ] ) && wp_verify_nonce( $_POST['faqs_questions_nonce'],basename(__FILE__)) )? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce )
{return;} //exit
if( isset( $_POST[ 'meta-select' ] ) )
{ update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] ); }
if( isset( $_POST[ 'meta-textarea' ] ) ) {
update_post_meta( $post_id, 'meta-textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) ); }
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox' ] ) ) {
update_post_meta( $post_id, 'meta-checkbox', 'yes' );}
else {
update_post_meta( $post_id, 'meta-checkbox', '' ); }
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox-two' ] ) ) {
update_post_meta( $post_id, 'meta-checkbox-two', 'yes' );}
else {
update_post_meta( $post_id, 'meta-checkbox-two', '' ); }
}
add_action('save_post','faq_meta_save');
You have to check if you have the meta_key duplicated in the database for the same post_id. Also, I recommend you to use underscores instead of hyphens in the meta_key names. Try this code where the meta_data of the post_id is deleted and then is added the new value.
Try this:
<?php
function faq_manage_things($post)
{
global $post;
$post_id= $post->ID;
wp_nonce_field(basename(__FILE__),'faqs_questions_nonce');
$faq_stored_meta= get_post_meta($post->ID);
?>
<div class="wrap">
<p>
<label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label>
<select name="meta-select" id="meta-select">
<option value="select-one" <?php if ( isset ( $prfx_stored_meta['meta_select'] ) ) selected( $prfx_stored_meta['meta_select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>';
<option value="select-two" <?php if ( isset ( $prfx_stored_meta['meta_select'] ) ) selected( $prfx_stored_meta['meta_select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>';
</select>
</p>
<!-- TEXT AREA -->
<p>
<label for="meta-textarea" class="prfx-row-title"><?php _e( 'Example Textarea Input', 'prfx-textdomain' )?></label>
<textarea name="meta-textarea" id="meta-textarea"><?php if ( isset ( $prfx_stored_meta['meta_textarea'] ) ) echo $prfx_stored_meta['meta_textarea'][0]; ?></textarea>
</p>
<!-- CHECKBOXES -->
<p>
<span class="prfx-row-title"><?php _e( 'Example Checkbox Input', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['meta_checkbox'] ) ) checked( $prfx_stored_meta['meta_checkbox'][0], 'yes' ); ?> /><?php _e( 'Checkbox label', 'prfx-textdomain' )?>
</label>
<label for="meta-checkbox-two">
<input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if ( isset ( $prfx_stored_meta['meta_checkbox_two'] ) ) checked( $prfx_stored_meta['meta_checkbox_two'][0], 'yes' ); ?> /><?php _e( 'Another checkbox', 'prfx-textdomain' )?>
</label>
</div>
</p>
</div>
<?php
}
global $post;
function faq_meta_save( $post_id ) {
global $post;
$post_id= $post->ID;
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'faqs_questions_nonce' ] ) && wp_verify_nonce( $_POST['faqs_questions_nonce'],basename(__FILE__)) )? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce )
{return;} //exit
if( isset( $_POST[ 'meta-select' ] ) ) {
remove_post_meta( $post_id, 'meta_select', $_POST[ 'meta-select' ] );
add_post_meta( $post_id, 'meta_select', $_POST[ 'meta-select' ] );
}
if( isset( $_POST[ 'meta-textarea' ] ) ) {
remove_post_meta( $post_id, 'meta_textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) );
add_post_meta( $post_id, 'meta_textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) );
}
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox' ] ) ) {
remove_post_meta( $post_id, 'meta_checkbox', 'yes' );
add_post_meta( $post_id, 'meta_checkbox', 'yes' );
}
else {
remove_post_meta( $post_id, 'meta_checkbox');
}
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox-two' ] ) ) {
remove_post_meta( $post_id, 'meta_checkbox-two', 'yes' );
add_post_meta( $post_id, 'meta_checkbox-two', 'yes' );
}
else {
remove_post_meta( $post_id, 'meta_checkbox-two');
}
}
add_action('save_post','faq_meta_save');

Categories