Why do my custom meta data fields clear after saving? - wordpress

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>';
}

Related

Wordpress custom meta field for custom post not storing data

I have created custom post type named as freebie in that i have created a custom meta section in that added a input field. Which is not storing the data which entered in that field also not displaying the values entered in that field. I have attached the coding.
function adding_freebie_metabox( $post ) {
add_meta_box( 'my-meta-box',__( 'Freebie extra deatails', 'lwprjs' ),'render_my_freebie_metabox','freebie','normal','default');
}
add_action( 'add_meta_boxes_freebie', 'adding_freebie_metabox' );
//Add field
function render_my_freebie_metabox( $meta_id ) {
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'freebie_meta_box_nonce' );
?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table"><tbody>
<tr>
<th><label for="freebie-demo">Demo URL</label></th>
<td><input style="width: 100%" id="freebie-demo" name="freebie-demo" type="text" value="<?php get_post_meta( $post->ID, $meta_field['freebie-demo'], true ); ?>"></td>
</tr>
</tbody></table>
<?php
}
function food_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['freebie_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['freebie_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// store custom fields values
// cholesterol string
if ( isset( $_REQUEST['freebie-demo'] ) ) {
update_post_meta( $post_id, '_freebie_demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}
}
add_action( 'save_post_freebie', 'food_save_meta_box_data' );
?>
You are saving with this:
freebie_demo
But then retrieving with this:
freebie-demo
So try changing your saving code to this:
if ( isset( $_POST['freebie-demo'] ) ) {
update_post_meta( $post_id, 'freebie-demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}
I rewrote the whole block, please check to see if it works now:
function adding_freebie_metabox( $post ) {
add_meta_box( 'my-meta-box',__( 'Freebie extra deatails', 'lwprjs' ),'render_my_freebie_metabox','freebie','normal','default');
}
add_action( 'add_meta_boxes_freebie', 'adding_freebie_metabox' );
//Add field
function render_my_freebie_metabox( $post ) {
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'freebie_meta_box_nonce' );
?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table"><tbody>
<tr>
<th><label for="freebie-demo">Demo URL</label></th>
<td><input style="width: 100%" id="freebie-demo" name="freebie-demo" type="text" value="<?php get_post_meta( $post->ID, '_freebie_demo', true ); ?>"></td>
</tr>
</tbody></table>
<?php
}
function food_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['freebie_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['freebie_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// store custom fields values
// cholesterol string
if ( isset( $_REQUEST['freebie-demo'] ) ) {
update_post_meta( $post_id, '_freebie_demo', sanitize_text_field( $_POST['freebie-demo'] ) );
}
}
add_action( 'save_post_freebie', 'food_save_meta_box_data' );
?>

Custom Meta Boxes in Wordpress are not updating - need help identifying the errors in this function

I created a small plugin to add a Color meta box to a custom post type. The box displays fine but I can't seem to get it to save the data. When I type something in the field and click update, the field returns blank. I researched this and followed many tutorials, and each offered a slightly different approach. The code below was the easiest for me to follow, so I would really appreciate any help with identifying the error(s) in it. This is my first question so sorry if I omitted anything relevant.
Here is how I added the box:
add_action( 'add_meta_boxes', 'addmeta' );
function addmeta() {
$post_types = array ('post', 'ev');
foreach ( $post_types as $post_type ) {
add_meta_box (
'color_box',
'Color',
'display_meta_box',
$post_type,
'side'
);
}
}
add_action ( 'add_meta_boxes', 'addmeta');
Function to display the meta box:
function display_meta_box() {
$value = get_post_meta( $post->ID, '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="my_text" id="my_text" />
<?php
}
And this is the save function:
function save_meta_box ( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$wp_valid_nonce = false;
if( isset( $_POST[ 'my_nonce' ] ) ) {
if ( wp_verify_nonce( $_POST['my_nonce'], basename( __FILE__ ) ) ) {
$is_valid_nonce = true;
}
}
if ( $is_autosave || $is_revision || !$is_valid_nonce ) return;
if( array_key_exists( 'color_box', $_POST ) ) {
update_post_meta(
$post_id,
'_mykey',
sanitize_text_field( $_POST[ 'color_box' ] )
);
}
}
add_action( 'save_post', 'save_meta_box' );
Thank you!
you need to define ID. Check this code it will work definitively.
function display_meta_box( ) {
$value = get_post_meta( get_the_ID(), '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="color_box" value="<?php echo $value;?>" id="my_text" />
<?php
}
You are putting wrong name in input box in your function display_meta_box. Replace your function with below function.
function display_meta_box() {
global $post;
$value = get_post_meta( $post->ID, '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="color_box" value="<?php echo $value;?>" id="my_text" />
<?php
}
Hope this will help. Please let me know if any issue.

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/

metabox is inserting only one value of checkbox

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

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