Checkboxes Not Saving to Database - php

In the function below, data in the text input is being saved to the database just as it is supposed to. The check boxes, however, are not staying checked and don't appear to be saving anything to the database. (The code is from a WordPress plugin I'm modifying to show or hide menu items based on a user's location.)
What am I doing wrong?
function option( $fields, $item_id ) {
ob_start(); ?>
<p class="field-visibility description description-wide">
<label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
<?php _e('Enter country code(s) separated by commas') ?>:
<input
type="text"
class="widefat code"
id="edit-menu-item-visibility-<?php echo $item_id; ?>"
name="menu-item-visibility[<?php echo $item_id; ?>]"
value="<?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?>" /></br>
<input
type="radio"
id="edit-menu-item-visibility-<?php echo $item_id;?>"
name="menu-item-show-hide[<?php echo $item_id; ?>]"
value="hide" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'hide', true ); ?>
/>Hide from these locations.</br>
<input
type="radio"
id="edit-menu-item-visibility-<?php echo $item_id; ?>"
name="menu-item-show-hide[<?php echo $item_id; ?>]"
value="show" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'show', true ); ?>
/>Only show to these locations.</br>
</label>
</p>
<?php
$fields[] = ob_get_clean();
return $fields;
}
Here's what I'm using to update the data in the database:
function update_option_text( $menu_id, $menu_item_db_id, $args ) {
$meta_value = get_post_meta( $menu_item_db_id, 'locations', true );
$new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] );
if( '' == $new_meta_value ) {
delete_post_meta( $menu_item_db_id, 'locations', $meta_value );
}
elseif( $meta_value !== $new_meta_value ) {
update_post_meta( $menu_item_db_id, 'locations', $new_meta_value );
}
}
function update_option_hide_show( $menu_id, $menu_item_db_id, $args ) {
$meta_value = get_post_meta( $menu_item_db_id, 'hide_show', true );
$new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] );
if( '' == $new_meta_value ) {
delete_post_meta( $menu_item_db_id, 'hide_show', $meta_value );
}
elseif( $meta_value !== $new_meta_value ) {
update_post_meta( $menu_item_db_id, 'hide_show', $new_meta_value );
}
}
And the constructor:
function __construct() {
if( is_admin() ) {
add_filter( 'wp_edit_nav_menu_walker', array( &$this, 'edit_nav_menu_walker' ) );
add_filter( 'wp_nav_menu_item_custom_fields', array( &$this, 'option' ), 12, 2 );
add_action( 'wp_update_nav_menu_item', array( &$this, 'update_option_text' ), 10, 3 );
add_action( 'wp_update_nav_menu_item', array( &$this, 'update_option_hide_show' ), 10, 3 );
add_action( 'delete_post', array( &$this, 'remove_visibility_meta' ), 1, 3);
} else {
add_filter( 'wp_get_nav_menu_items', array( &$this, 'visibility_check' ), 10, 3 );
add_action( 'init', array( &$this, 'clear_gantry_menu_cache' ) );
}
}

Changing $new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] ); to $new_meta_value = stripcslashes( $_POST['menu-item-show-hide'][$menu_item_db_id] ); did the trick. It's working perfectly now!
Thanks #Thomas for making me rethink how it was getting posted to the database.

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.

Vertical and expanded product tabs in WooCommerce

I used the answer from this question to create my custom product tabs:
Custom metabox content displayed in single product additional tabs on Woocommerce
Here's my code:
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Shipping
echo '<h4>' . __( 'Shipping', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_shipping', true );
wp_editor( $value, '_custom_shipping', array( 'editor_height' => 100 ) );
// Color
echo '<br><hr><h4>' . __( 'Color', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_color', true );
wp_editor( $value, '_custom_color', array( 'editor_height' => 100 ) );
// Material
echo '<br><hr><h4>' . __( 'Material', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_material', true );
wp_editor( $value, '_custom_material', array( 'editor_height' => 100 ) );
// Size
echo '<br><hr><h4>' . __( 'Size', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_size', true );
wp_editor( $value, '_custom_size', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_custom_shipping' ]) )
update_post_meta( $post_id, '_custom_shipping', wp_kses_post($_POST[ '_custom_shipping' ]) );
if( isset($_POST[ '_custom_color' ]) )
update_post_meta( $post_id, '_custom_color', wp_kses_post($_POST[ '_custom_color' ]) );
if( isset($_POST[ '_custom_material' ]) )
update_post_meta( $post_id, '_custom_material', wp_kses_post($_POST[ '_custom_material' ]) );
if( isset($_POST[ '_custom_size' ]) )
update_post_meta( $post_id, '_custom_size', wp_kses_post($_POST[ '_custom_size' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// 1) Removing tabs
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
// 2 Adding new tabs and set the right order
//Adds Shipping Tab
$tabs['custom_shipping_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_custom_shipping_tab_content'
);
// Adds Color Tab
$tabs['custom_color_tab'] = array(
'title' => __( 'Color', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_custom_color_tab_content'
);
// Adds Material Tab
$tabs['custom_material_tab'] = array(
'title' => __( 'Material', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_custom_material_tab_content'
);
// Adds Size Tab
$tabs['custom_size_tab'] = array(
'title' => __( 'Size', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_custom_size_tab_content'
);
return $tabs;
}
function woo_custom_shipping_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_shipping' ) . '</p></div>';
}
function woo_custom_color_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_color' ) . '</p></div>';
}
function woo_custom_material_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_material' ) . '</p></div>';
}
function woo_custom_size_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_size' ) . '</p></div>';
}
Then I added custom code to create vertical tabs and added them after woocommerce_share hook.
/* Vertical Product Tabs */
add_action( 'woocommerce_share', 'woocommerce_output_product_data_tabs', 5 );
function woocommerce_output_product_data_tabs() {
$product_tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( empty( $product_tabs ) ) {
return;
}
foreach ( $product_tabs as $key => $product_tab ) {
echo '<div id="tab-' . esc_attr( $key ) . '">';
if ( isset( $product_tab[ 'callback' ] ) ) {
call_user_func( $product_tab[ 'callback' ], $key, $product_tab );
}
echo '</div>';
}
}
The result was as follows:
All the tabs have lost titles.
Now tabs are shown after woocommerce_share hook and in their old place. They are just copied.
Question:
How do I fix the code to show the tab titles?
How to fix the code so that the tabs are shown only after the woocommerce_share hook?
I would be glad to have your help!
First of all, I've slightly updated your existing code, to more recent version:
// You can use add_meta_boxes_{post_type} for best practice, so your hook will only run when editing a specific post type. This will only receive 1 parameter – $post
function action_add_meta_boxes_product( $post ) {
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
add_action( 'add_meta_boxes_product', 'action_add_meta_boxes_product', 10, 1 );
// Add custom metabox content
function additional_product_tabs_metabox_content( $post ) {
// Shipping
echo '<h4>' . __( 'Shipping', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_shipping', true );
wp_editor( $value, '_custom_shipping', array( 'editor_height' => 100 ) );
// Color
echo '<br><hr><h4>' . __( 'Color', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_color', true );
wp_editor( $value, '_custom_color', array( 'editor_height' => 100 ) );
// Material
echo '<br><hr><h4>' . __( 'Material', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_material', true );
wp_editor( $value, '_custom_material', array( 'editor_height' => 100 ) );
// Size
echo '<br><hr><h4>' . __( 'Size', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_size', true );
wp_editor( $value, '_custom_size', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
function action_save_post_product( $post_id, $post, $update ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values
if ( isset( $_POST[ '_custom_shipping' ] ) )
update_post_meta( $post_id, '_custom_shipping', wp_kses_post( $_POST[ '_custom_shipping' ] ) );
if ( isset( $_POST[ '_custom_color' ] ) )
update_post_meta( $post_id, '_custom_color', wp_kses_post( $_POST[ '_custom_color' ] ) );
if ( isset( $_POST[ '_custom_material' ] ) )
update_post_meta( $post_id, '_custom_material', wp_kses_post( $_POST[ '_custom_material' ] ) );
if ( isset( $_POST[ '_custom_size' ] ) )
update_post_meta( $post_id, '_custom_size', wp_kses_post( $_POST[ '_custom_size' ] ) );
}
add_action( 'save_post_product', 'action_save_post_product', 10, 3 );
Then your biggest mistake is using $product_tabs = apply_filters( 'woocommerce_product_tabs', array() );. This is because not only your call is executed but also the default call of WooCommerce. That's why these are shown twice.
The solution is to simply add your custom tabs via an array that is separate from the default WooCommerce functionality.
So you get:
// Removing DEFAULT tabs
function filter_woocommerce_product_tabs( $tabs ) {
// Remove the description tab
unset( $tabs['description'] );
// Remove the additional information tab
unset( $tabs['additional_information'] );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
function my_custom_tabs() {
// Initialize
$tabs = array();
// Adds Shipping Tab
$tabs['custom_shipping_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_custom_shipping_tab_content'
);
// Adds Color Tab
$tabs['custom_color_tab'] = array(
'title' => __( 'Color', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_custom_color_tab_content'
);
// Adds Material Tab
$tabs['custom_material_tab'] = array(
'title' => __( 'Material', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_custom_material_tab_content'
);
// Adds Size Tab
$tabs['custom_size_tab'] = array(
'title' => __( 'Size', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_custom_size_tab_content'
);
return $tabs;
}
function woo_custom_shipping_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_shipping' ) . '</p></div>';
}
function woo_custom_color_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_color' ) . '</p></div>';
}
function woo_custom_material_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_material' ) . '</p></div>';
}
function woo_custom_size_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_size' ) . '</p></div>';
}
function action_woocommerce_share() {
// Call your custom tabs
$product_tabs = my_custom_tabs();
// NOT empty
if ( ! empty( $product_tabs ) ) : ?>
<div class="woocommerce-tabs wc-tabs-wrapper">
<ul class="tabs wc-tabs" role="tablist">
<?php foreach ( $product_tabs as $key => $product_tab ) : ?>
<li class="<?php echo esc_attr( $key ); ?>_tab" id="tab-title-<?php echo esc_attr( $key ); ?>" role="tab" aria-controls="tab-<?php echo esc_attr( $key ); ?>">
<a href="#tab-<?php echo esc_attr( $key ); ?>">
<?php echo wp_kses_post( apply_filters( 'woocommerce_product_' . $key . '_tab_title', $product_tab['title'], $key ) ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php foreach ( $product_tabs as $key => $product_tab ) : ?>
<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--<?php echo esc_attr( $key ); ?> panel entry-content wc-tab" id="tab-<?php echo esc_attr( $key ); ?>" role="tabpanel" aria-labelledby="tab-title-<?php echo esc_attr( $key ); ?>">
<?php
if ( isset( $product_tab['callback'] ) ) {
call_user_func( $product_tab['callback'], $key, $product_tab );
}
?>
</div>
<?php endforeach; ?>
<?php do_action( 'woocommerce_product_after_tabs' ); ?>
</div>
<?php endif;
}
add_action( 'woocommerce_share', 'action_woocommerce_share' );
Note: displaying the tabs horizontally or vertically is then a matter of applying CSS, this is theme related. Optionally you can add extra classes to the output of these tabs.

Be able to select multiple values in "select" or "checkbox" wordpress

How to select multiple values in listbox. I have tried with the below code, it selects the value only once. Please suggest and provide an alternative way to select the multiple values at the time:
I work with wordpress, this is my code:
// Add metabox
add_action( 'add_meta_boxes', 'cpt_news_filiale_meta_box_add' );
function cpt_news_filiale_meta_box_add()
{
add_meta_box( 'gps-meta-box-id', 'Filiale', 'cpt_entreprise_news_filiale_meta_box_display', 'newsdesfiliales', 'normal', 'high' );
}
// Dislay metabox
function cpt_entreprise_news_filiale_meta_box_display( $post )
{
$args = array(
'numberposts' => 999,
'post_type' => 'entreprise_sector',
'status' => 'publish',
'suppress_filters' => 0,
);
$entreprises = get_posts( $args );
$values = get_post_custom( $post->ID );
$text = isset( $values['filiale_referente[]'] ) ? esc_attr( $values['filiale_referente'][0] ) : '';
?>
<p>
<label for="filiale_referente">Filiale référente : </label>
<select name='filiale_referente' id='filiale_referente' multiple>
<?php foreach ($entreprises as $entreprise): ?>
<option <?php if($text == $entreprise->ID ) :?> selected="true" <?php endif;?>value="<?php echo esc_attr($entreprise->ID); ?>"><?php echo esc_html($entreprise->post_title); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
// Save metabox
add_action( 'save_post', 'cpt_news_filiale_meta_box_save' );
function cpt_news_filiale_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['filiale_referente'] ) )
update_post_meta( $post_id, 'filiale_referente', wp_kses( $_POST['filiale_referente'] ) );
}
Try this code
// Add metabox
add_action( 'add_meta_boxes', 'cpt_news_filiale_meta_box_add' );
function cpt_news_filiale_meta_box_add()
{
add_meta_box( 'gps-meta-box-id', 'Filiale', 'cpt_entreprise_news_filiale_meta_box_display', 'newsdesfiliales', 'normal', 'high' );
}
// Dislay metabox
function cpt_entreprise_news_filiale_meta_box_display( $post )
{
$args = array(
'numberposts' => 999,
'post_type' => 'entreprise_sector',
'status' => 'publish',
'suppress_filters' => 0,
);
$entreprises = get_posts( $args );
$values = get_post_meta( $post->ID,'filiale_referente', true);
$valuesArr = explode(",",$values);
?>
<p>
<label for="filiale_referente">Filiale référente : </label>
<select name='filiale_referente[]' id='filiale_referente' multiple>
<?php foreach ($entreprises as $entreprise): ?>
<option <?php if( in_array($entreprise->ID,$valuesArr)) :?> selected="true" <?php endif;?>value="<?php echo esc_attr($entreprise->ID); ?>"><?php echo esc_html($entreprise->post_title); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
// Save metabox
add_action( 'save_post', 'cpt_news_filiale_meta_box_save' );
function cpt_news_filiale_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['filiale_referente'] ) )
update_post_meta( $post_id, 'filiale_referente', implode(",",$_POST['filiale_referente']) );
}

Wordpress - Custom post image on frontend doesn't upload

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.

Second excerpt in Wordpress

I have created second excerpt in Wordpress with following code.
add_action( 'admin_menu', 'my_create_post_meta_box' );
add_action( 'save_post', 'my_save_post_meta_box', 10, 2 );
function my_create_post_meta_box() {
add_meta_box( 'my-meta-box', 'Second Excerpt', 'my_post_meta_box', 'post', 'normal', 'high' );
}
function my_post_meta_box( $object, $box ) { ?>
<p>
<?php if ( current_user_can( 'upload_files' ) ) : ?>
<div id="media-buttons" class="hide-if-no-js">
<?php do_action( 'media_buttons' ); ?>
</div>
<?php endif; ?>
<br />
<label for="second-excerpt">Second Excerpt With Images for Post List Page</label>
<textarea name="second-excerpt" id="second-excerpt" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( get_post_meta( $object->ID, 'Second Excerpt', true ), 1 ); ?></textarea>
<input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
function my_save_post_meta_box( $post_id, $post ) {
if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
$meta_value = get_post_meta( $post_id, 'Second Excerpt', true );
$new_meta_value = stripslashes( $_POST['second-excerpt'] );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );
elseif ( $new_meta_value != $meta_value )
update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
}
Now, How will I be able to display it instead of the entire post content? My wordpress home page shows latest posts instead of any static page.
You'll want to use get_post_meta() for that:
echo get_post_meta( get_the_ID(), 'Second Excerpt', true );
If you're wanting to replace the post content with this second excerpt, you'll need to write a custom the_content filter.

Categories