$post_id or $post->ID in wordpress meta box construction - wordpress

Can someone explain why when creating meta boxes the callback needs the post id to be passed via $post->ID, however with the action hook 'save_post', the function can pass $post_id. A general explanation of when to use which one would help clear up some issues I've been having, thanks.
ex:
function show_custom_meta_box($post) {
$meta = get_post_meta($post->ID, 'custom_meta_class', true);
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
echo '<tr>
<th><label for="custom-meta-class">Custom Meta Box</label></th>
<td>
<input class="widefat" type="text" name="custom-meta-class" id="custom-meta-class" value="'.$meta.'" size="50" />';
echo '</td></tr>';
echo '</table>'; // end table
}
and for the $post_id
function save_custom_meta($post) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['custom_meta_box_nonce'], basename( __FILE__ ) ) )
return $post_id;
$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['custom-meta-class'] ) ? sanitize_html_class( $_POST['custom-meta-class'] ) : '' );
/* Get the meta key. */
$meta_key = 'custom_meta_class';
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
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 );
}

I don't have an answer but I have the exact same problem. I'm surprised nobody has chimed in in the last 7 years.
Here's my code, it works (in functions.php)
add_action('save_post','extract_citations',100,1);
function extract_citations($post_id){
$the_post = get_post($post_id);
$content = $the_post->post_content;
preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',$content,$citation_array);
$citation_urls = $citation_array[1];
update_post_meta($post_id,'citations',serialize($citation_urls));
}
Here is code that does NOT work, despite being theoretically correct:
add_action('save_post','extract_citations',100,1);
function extract_citations($post){
$the_post = get_post($post->ID);
$content = $the_post->post_content;
preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',$content,$citation_array);
$citation_urls = $citation_array[1];
update_post_meta($post->ID,'citations',serialize($citation_urls));
}

I know this is a very old thread, but I wanted to add my thoughts.
Let’s pretend that the Post ID is 1724
In the first example you are inserting 1724 directly into the function so line 3 would become
$the_post = get_post(1724);
While in the second example of are asking for the ID of the post number so your line 3 would become
$the_post = get_post(1703->ID);
The thing to remember is that when you call the function within your site the number is added which becomes $post_id, it doesn’t actually matter what word you put in the brackets of the function.

Related

Save custom data to customer and pre fill value on next orders

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.

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

WordPress: Custom Post Type Meta boxes all have same value after save (How to add multiple meta boxes)

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

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

Print Metabox Data to footer?

I followed the tutorial here http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/
and I have it working as intended in the tutorial, but instead of adding the class I just want to add the content to the footer.php file:
Here is the code from the tutorial that I have in functions.php
/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'smashing_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'smashing_post_meta_boxes_setup' );
/* Meta box setup function. */
function smashing_post_meta_boxes_setup() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'smashing_add_post_meta_boxes' );
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'smashing_save_post_class_meta', 10, 2 );
}
/* Create one or more meta boxes to be displayed on the post editor screen. */
function smashing_add_post_meta_boxes() {
add_meta_box(
'smashing-post-class', // Unique ID
esc_html__( 'Post Class', 'example' ), // Title
'smashing_post_class_meta_box', // Callback function
'post', // Admin page (or post type)
'side', // Context
'default' // Priority
);
}
/* Display the post meta box. */
function smashing_post_class_meta_box( $object, $box ) { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>
<p>
<label for="smashing-post-class"><?php _e( "Add a custom CSS class, which will be applied to WordPress' post class.", 'example' ); ?></label>
<br />
<input class="widefat" type="text" name="smashing-post-class" id="smashing-post-class" value="<?php echo esc_attr( get_post_meta( $object->ID, 'smashing_post_class', true ) ); ?>" size="30" />
</p>
<?php }
/* Save the meta box's post metadata. */
function smashing_save_post_class_meta( $post_id, $post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'], basename( __FILE__ ) ) )
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['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : '' );
/* Get the meta key. */
$meta_key = 'smashing_post_class';
/* Get the meta value of the custom field key. */
$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 );
}
/* Filter the post class hook with our custom post class function. */
add_filter( 'post_class', 'smashing_post_class' );
function smashing_post_class( $classes ) {
/* Get the current post ID. */
$post_id = get_the_ID();
/* If we have a post ID, proceed. */
if ( !empty( $post_id ) ) {
/* Get the custom post class. */
$post_class = get_post_meta( $post_id, 'smashing_post_class', true );
/* If a post class was input, sanitize it and add it to the post class array. */
if ( !empty( $post_class ) )
$classes[] = sanitize_html_class( $post_class );
}
return $classes;
}
how do I just echo the data entered into the field to my footer.php without anything extra?
<?--php global $post; echo get_post_meta($post->ID,'smashing-post-class',true) ?>
</body>
</html>
In your footer.php, you could call global $post to access info stored there. For instance:
<?--php
global $post;
echo get_post_meta($post->ID,'smashing-post-class',true) ?>

Resources