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/
Related
I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
A WooCommerce plugin provides custom order data (a Fedex number) from checkout so clients don't have to pay shipping costs. I need this value (fedex number) saved to the customer so next time a order is created in backend the Fedex number is pre-filled.
I created a meta box in WooCommerce Order admin which shows the value and also created a field beneath Billing details which shows the value.
I can't figure out how to get the value pre-filled when creating a new order in backend.
Here's what I got so far:
//* Display field value on the order edit page *//
add_action( 'woocommerce_admin_order_data_after_billing_address',
'my_fedex_checkout_field_display_admin_order_meta', 10, 1 );
function my_fedex_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('FedEx number Client').':</strong> ' .
get_post_meta( $order->id, 'FedEx_number', true ) . '</p>';
}
//* Adding Meta container admin shop_order pages *//
add_action( 'add_meta_boxes', 'fedex_add_meta_boxes' );
if ( ! function_exists( 'fedex_add_meta_boxes' ) )
{
function fedex_add_meta_boxes()
{
global $woocommerce, $order, $post;
add_meta_box( 'fedex_other_fields', __('FedEx number
Client','woocommerce'), 'fedex_add_other_fields_for_orders',
'shop_order', 'side', 'core' );
}
}
//* adding Meta field in the meta container admin shop_order pages
//*
if ( ! function_exists( 'fedex_save_wc_order_other_fields' ) )
{
function fedex_add_other_fields_for_orders()
{
global $woocommerce, $order, $post;
$meta_field_data = get_post_meta( $post->ID, 'FedEx_number',
true ); //? get_post_meta( $post->ID, 'FedEx_number', true ) : '';
echo '<input type="hidden" name="fedex_other_meta_field_nonce"
value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";"
name="FedEx_number" placeholder="' . $meta_field_data . '" value="'
. $meta_field_data . '"></p>';
}
}
//* Save Fedex number to Customer //*
add_action('save_post_shop_order', 'customer_fedex_save', 50, 3 );
function customer_fedex_save( $post_id, $post, $update ) {
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and
'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
if( isset($_POST['FedEx_number']) ) {
$order = wc_get_order( $post_id );
// Update user meta data
update_user_meta( $order->get_customer_id(), 'FedEx_number',
sanitize_text_field( $_POST['FedEx_number'] ) );
}
}
I have made some changes to your code adding also some additional functions. As I don't use your Fedex plugin, I have tested this partially and I am not really sure that it will work as you would like.
The code:
// On Order submit: Save 'FedEx_number' as user meta data if it doesn't exits
add_action( 'woocommerce_checkout_update_order_meta', 'sync_fedex_checkout_field_to_order', 100, 1 );
function sync_fedex_checkout_field_to_order( $order_id, $data ){
$meta_key = 'FedEx_number';
$user_id = (int) get_post_meta( $order_id, '_customer_user', true );
if( $user_id > 0 ) {
$meta_value = get_user_meta( $user_id, $meta_key, true );
}
// Sync 'FedEx_number' as user meta data if it doesn't exist yet
if( $user_id > 0 && ! $meta_value ) {
// Get the 'FedEx_number' value and update user data
if( $meta_value = get_post_meta( $order_id, $meta_key, true ) )
update_user_meta( $user_id, $meta_key, esc_attr( $meta_value ) );
}
}
// Display the fedex field value under admin order billing address section
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_fedex_checkout_field_display_admin_order_meta', 10, 1 );
function my_fedex_checkout_field_display_admin_order_meta( $order ){
$meta_key = 'FedEx_number';
$user_id = $order->get_customer_id();
if( $user_id > 0 ) {
$meta_value = get_user_meta( $user_id, $meta_key, true );
}
if( ! isset($meta_value) ) {
$meta_value = $order->get_meta( $meta_key );
}
if( isset($meta_value) ) {
echo '<p><strong>'.__("FedEx number Client", "woocommerce").':</strong> ' . $meta_value . '</p>';
}
}
// Add fedex order metabox
add_action( 'add_meta_boxes', 'add_fedex_meta_box' );
function add_fedex_meta_box() {
global $post;
add_meta_box( 'fedex_meta_box',
__("FedEx number Client", "woocommerce'"),
'fedex_meta_box_content',
'shop_order', 'side', 'core' );
}
// The fedex metabox content
function fedex_meta_box_content() {
global $post;
$meta_key = 'FedEx_number';
$user_id = get_post_meta( $post->ID, '_customer_user', true );
if( $user_id > 0 ) {
$meta_value = get_user_meta( $user_id, $meta_key, true );
} else {
$meta_value = get_post_meta( $post->ID, $meta_key, true );
}
echo '<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;" name="'.$meta_key.'" value="'.$meta_value.'">
<input type="hidden" name="fedex_field_nonce" value="' . wp_create_nonce() . '"></p>';
}
add_action('save_post_shop_order', 'customer_fedex_save', 100, 1 );
function customer_fedex_save( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'fedex_field_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'fedex_field_nonce' ] ) ) {
return $post_id;
}
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return $post_id;
}
$meta_key = 'FedEx_number';
if( isset($_POST[$meta_key]) ) {
$user_id = (int) get_post_meta( $post_id, '_customer_user', true );
// Update post meta
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[$meta_key] ) );
// Update user meta
if( $user_id > 0)
update_user_meta( $user_id, $meta_key, sanitize_text_field( $_POST[$meta_key] ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested partially.
I have a custom post type, vehicles, I have added two custom meta boxes with the following code (from the codex). The only problem is that when I save they both get the value of the second box. I have searched but cannot find out how to same them as different meta values.
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function lm_add_meta_box() {
add_meta_box(
'lm_vehicles_capacity',
__( 'Capacity', 'lm_textdomain' ),
'lm_meta_box_callback1',
'vehicles',//$screen
'side',
'high'
);
add_meta_box(
'lm_vehicles_upselltext',
__( 'Upsell Text', 'lm_textdomain' ),
'lm_meta_box_callback2',
'vehicles',//$screen
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function lm_meta_box_callback1( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );
echo '<label for="lm_new_field1">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );
echo '<label for="lm_new_field2">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field2" name="lm_new_field2" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function lm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' ) ) {
return;
}
// 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;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
return;
}
// Sanitize user input.
$my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
$my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );
OK there were quite some mistakes in your code. I think you just pulled it from the codex and added a new filed at the top :P. I haven't tested it yet, but all the changes that i think were relevant have been added. There still might be a few minor adjustments to make.
<?php /**
* Adds a box to the main column on the Post and Page edit screens.
*/
function lm_add_meta_box() {
add_meta_box(
'lm_vehicles_capacity',
__( 'Capacity', 'lm_textdomain' ),
'lm_meta_box_callback1',
'vehicles',//$screen
'side',
'high'
);
add_meta_box(
'lm_vehicles_upselltext',
__( 'Upsell Text', 'lm_textdomain' ),
'lm_meta_box_callback2',
'vehicles',//$screen
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function lm_meta_box_callback1( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );
echo '<label for="lm_new_field1">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );
echo '<label for="lm_new_field2">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field2" name="lm_new_field2" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function lm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' ) ) {
return;
}
// 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;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
return;
}
// Sanitize user input.
$my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
$my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );
?>
from codex here is what add_meta_box looks like
add_meta_box( $id, $title, $callback, $screen, $context,
$priority, $callback_args );
you are using same callback for both metabox and the last one metabox value gets used change callback for both of them .it will work perefect
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' );
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>';
}