metabox is inserting only one value of checkbox - wordpress

only one checkbox value is inserted on database,i want multiple checkbox value insert on database.
What i want that, Multiple checkbox should insert on database and after insertion on database, inserted checkbox should be checked.Please help
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID ); ?>
<form method="post">
<?php
$users = get_users();
foreach ($users as $user){ ?>
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox[]" id="meta-checkbox" value="<?php echo $user->user_login; ?>" <?php if ( isset ( $prfx_stored_meta['meta-checkbox'] ) ) checked( $prfx_stored_meta['meta-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Checkbox label', 'prfx-textdomain' )?>
</label>
<?php
} ?>
</form>
<?php
}
function prfx_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[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
$checkbox = $_POST['meta-checkbox'];
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox' ] ) ) {
for($i=0;$i<sizeof($checkbox);$i++){
update_post_meta( $post_id, 'meta-checkbox', $checkbox[$i] );
}
} else {
update_post_meta( $post_id, 'meta-checkbox', '' );
}
}
add_action( 'save_post', 'prfx_meta_save' );

Related

Multiple user role to a custom post type

I've created a CPT, issue_chapter, and a new user translator. Now how a dropdown can be added with list of translators in this CPT? "Author" and "Translator" both should be there.
I have created a metabox
add_meta_box(
'translator',
'Translator',
'zc_translator_meta_box_callback',
'issue_chapter',
'advanced',
'high' );
function zc_translator_meta_box_callback( $post ) {
$role = 'translator';
$query_users_ids_by_role = array(
'field' => 'id',
'role' => $role
);
$array_of_users_ids = get_users( $query_users_ids_by_role );
$users_ids_list = implode( ',',$array_of_users_ids );
$query_for_dropdown = array(
'include' => $user_ids_list,
);
wp_dropdown_users( $query_for_dropdown );
}
Now how to save the value?
Thank you.
This is the solution. Thank you.
add_meta_box( 'the_translator',
'Translator',
'zc_the_translator_callback',
'issue_chapter',
'advanced',
'high'
);
add_action( 'save_post', 'zc_save_the_translator_data' );
function zc_the_translator_callback( $post ){
wp_nonce_field( 'zc_save_the_translator_data', 'zc_the_translator_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_the_translator_value_key', true );
?>
<label for="zc_the_translator_field">Translator</label>
<select name="zc_the_translator_field" id="zc_the_translator_field">
<option value="0">-- Select Translator --</option>
<?php
$args = array(
'role' => 'translator'
);
$user_list = get_users($args);
foreach( $user_list as $user ) : setup_postdata($post); ?>
<option value="<?php echo $user->ID; ?>" <?php selected($value, $user->ID); ?>><?php echo $user->user_nicename ; ?></option>
<?php endforeach; ?>
</select>
<?php
}
function zc_save_the_translator_data( $post_id ){
if( !isset( $_POST['zc_the_translator_meta_box_nonce']) ){
return;
}
if( !wp_verify_nonce( $_POST['zc_the_translator_meta_box_nonce'], 'zc_save_the_translator_data' ) ){
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
if( !current_user_can( 'edit_post', $post_id ) ){
return;
}
if( !isset( $_POST['zc_the_translator_field']) ){
return;
}
$my_data = sanitize_text_field( $_POST['zc_the_translator_field'] ) ;
update_post_meta( $post_id, '_the_translator_value_key', $my_data );
}
To retrieve the value use this:
<?php echo get_post_meta( $post->ID, '_the_translator_value_key', true ); ?>

woocommerace after checkout field is no validate ...when un check create account its still ask for full fill and validation

woocommerace after checkout field is no validate ...when un check create account its still ask for full fill and validation error i want when i checked create account then this validation will work
before un checked why this is asking for validation hope got my point
function
public function wooc_create_dobc_fields() {
$birthday_label = get_option( 'label_birthday' );
$birthday_required = get_option( 'required_fields_dobc' );
if ( empty( $birthday_label ) ) {
$birthday_label = 'Enter Your Date Of Birthday';
}
$user_id = get_current_user_id();
$show_date = get_user_meta( $user_id, 'dobc_date_field', true );
get_user_meta( $user_id, 'dobc_date_field', true );
?>
<div class="create-account">
<p>
<label for="label_birthday"><?php echo esc_attr( $birthday_label, 'dobc' ); ?>
<?php if ( 'yes' === $birthday_required ) : ?>
<span class="required" style="color:red;">*</span>
<?php endif; ?>
</label>
<?php $required = ( 'yes' === $birthday_required ) ? 'required' : ''; ?>
<input type="date" class="input-text" name="dobc_date_field" id="date of birthd " value="<?php echo esc_html( $show_date ); ?>" <?php echo esc_html( $required ); ?> />
<?php wp_nonce_field( 'birthday_nonce_action_checkout', 'birday_fields_checkout_nonce' ); ?>
</p>
</div>
<div class="clear"></div>
<?php
}
hooks
add_action( 'woocommerce_register_form', array( $this, 'wooc_create_dobc_fields' ) );
add_action( 'woocommerce_checkout_process', array( $this,'my_custom_checkout_field_process' ) );
call back validation
public function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( isset($_POST['dobc_date_field'] ) && empty($_POST['dobc_date_field'] ))
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}
i got the solution from my team leader
hooks
if ( 'yes' === $birthday_required ) {
if ( 'yes' == get_option( 'woocommerce_enable_myaccount_registration' ) ) {
add_action( 'woocommerce_checkout_process', array( $this, 'my_custom_checkout_field_process' ) );
call back function
public function my_custom_checkout_field_process() {
if ( isset( $_POST['birday_fields_checkout_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['birday_fields_checkout_nonce'], 'birthday_nonce_action_checkout' ) ) )
) {
print 'Sorry, your nonce did not verify.';
exit;
}
// Check if set, if its not set add an error.
if ( isset( $_POST['createaccount'] ) && empty( $_POST['dobc_date_field'] ) ) {
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}
}

How to add custom filed and save it in wordpress?

Following code I tried out But it is not save value of custom Fileds.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' );
}
In above I have add code it is display when posttype is product
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : ”;
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
In above code it will added the metabox with textbox and if value in metabox then it is display
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
}
I want to save data in a wp_postmeta table. Above code i have tried out. I am beginner in wordpress.Can any give me suggestion ?
Use This code for creating custom fields for post.
class Rational_Meta_Box {
private $screens = array(
'post',
);
private $fields = array(
array(
'id' => 'custom-field-1',
'label' => 'custom field 1',
'type' => 'text',
),
array(
'id' => 'custom-field-2',
'label' => 'custom field 2',
'type' => 'text',
),
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}
/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
'my-custom-fields',
__( 'my custom fields', 'wordpress' ),
array( $this, 'add_meta_box_callback' ),
$screen,
'advanced',
'high'
);
}
}
/**
* Generates the HTML for the meta box
*
* #param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
echo 'its for custom fields for post typle';
$this->generate_fields( $post );
}
/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
$db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
switch ( $field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$field['type'] !== 'color' ? 'class="regular-text"' : '',
$field['id'],
$field['id'],
$field['type'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
return $post_id;
$nonce = $_POST['my_custom_fields_nonce'];
if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field['id'] ] ) ) {
switch ( $field['type'] ) {
case 'email':
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
break;
case 'text':
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
break;
}
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkbox' ) {
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
}
}
}
}
new Rational_Meta_Box;
So it will create custom fields in your post type and also saved it too.
follow this for the references https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/
Just replace your add_action( 'save_post', 'cd_meta_box_save' ); function and put below code.
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Full code like ( it's working fine and also save custom field in postmeta table)
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high' );
}
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : '';
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Take a look at Pods.
It is a very powerful plugin for customization.
http://pods.io/

Why do my custom meta data fields clear after saving?

I've added a custom meta field to my Wordpress post type. Everytime I save, the fields don't hold the information (however the information is saved to the database).
My code:
function add_post_meta() {
add_meta_box(
'my_meta_box',
__( 'Metabox', 'framework' ),
'meta_box_content',
'post_type',
'advanced',
'high'
);
}
add_action( 'add_meta_boxes', 'add_post_meta' );
function virtual_merchant_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'meta_box_content_nonce' );
echo '<table><tr>';
echo '<td><label for="input_value">Enter Input Value:</label></td>';
echo '<td><input type="text" id="input_value" name="input_value" /></td>';
echo '</tr></table>';
}
function meta_box_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST['meta_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
$input_value = $_POST['input_value'];
update_post_meta( $post_id, 'input_value', $input_value );
}
add_action( 'save_post', 'metat_box_save' );
Get the input value using get_post_meta(), and then add it to the value="" attribute of the text input field.
function virtual_merchant_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'meta_box_content_nonce' );
$input_value = get_post_meta( $post->ID, 'input_value', true);
echo '<table><tr>';
echo '<td><label for="input_value">Enter Input Value:</label></td>';
echo '<td><input type="text" id="input_value" name="input_value" value="' . $input_value . '" /></td>';
echo '</tr></table>';
}

Why doesn't my metabox save its values?

I know there are many questions about variants of this, but I have not been able to find an answer.
I have a metabox on the post type page, containing nothing but a checkbox. It seems like it won't save no matter what I do. Here is all the code for the metabox.
/*--------------------------------------------------------------------------*
* Register metabox
/*--------------------------------------------------------------------------*/
function kasparabi_page_left_menu() {
add_meta_box( 'kasparabi-left-menu-meta', __( 'Left Menu', 'kasparabi' ), 'kasparabi_render_left_menu_meta_box', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'kasparabi_page_left_menu' );
/*--------------------------------------------------------------------------*
* Callbacks
/*--------------------------------------------------------------------------*/
function kasparabi_render_left_menu_meta_box($post) {
wp_nonce_field( basename( __FILE__ ), 'kasparabi-left-menu-meta_nonce' );
?>
<p>
<div>
<label for="left-menu-checkbox">
<input type="checkbox" name="left-menu-checkbox" <?php (get_post_meta( $post->ID, 'left_menu_checkbox', true) == 'on') ? ' checked="checked"' : ''; ?> />
<?php _e( 'Display left menu', 'kasparabi' )?>
</label>
</div>
</p>
<?php
}
/*--------------------------------------------------------------------------*
* Save functions
/*--------------------------------------------------------------------------*/
function kasparibi_left_menu_meta_save( $post_id, $post ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'kasparabi-left-menu-meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'kasparabi-left-menu-meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return $post_id;
}
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST['left-menu-checkbox'] ) ? sanitize_html_class( $_POST['left-menu-checkbox'] ) : '' );
$meta_key = 'left_menu_checkbox';
$meta_value = get_post_meta( $post->ID, $meta_key, true);
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true);
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
add_action( 'save_post', 'kasparibi_left_menu_meta_save' );
Turns out I didn't echo out the checked value, and that I needed to specify how many parameters the save function should receive.
Check this post here: https://wordpress.stackexchange.com/questions/126539/why-does-not-my-metabox-save

Resources