Print Metabox Data to footer? - wordpress

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

Related

How to list custom type post in metabox?

I've created 2 custom type post in Wordpress. Now i what to create custom metabox in one of them to select posts from second custom type post and display it in front. I cannot find how to figure out this problem, i've tried query post but nothing was displayed.
Please check the code to create Metabox named Company Address for example seller custom post type.
Meta box company address for seller custom post type added:
/**
* Meta box company address for seller custom post type added
*/
function wdbs_add_seller_metaboxes() {
add_meta_box(
'wdbs_seller_company_address',
'Company Address',
'wdbs_seller_company_address',
'seller',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'wdbs_add_seller_metaboxes' );
Meta box field company address html:
/**
* Meta box field company address html
*/
function wdbs_seller_company_address() {
global $post;
wp_nonce_field( basename( __FILE__ ), 'seller_fields' );
$company_address = get_post_meta( $post->ID, 'company_address', true );
echo '<textarea type="text" name="company_address" class="widefat" rows="6">' . esc_textarea( $company_address ) . '</textarea>';
}
Save Seller metafields:
/**
* SAVE SELLER METAFIELDS
* Saves values for company address meta field
*/
function wdbs_save_seller_meta( $post_id, $post ) {
// Return if the user doesn't have edit permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if ( ! isset( $_POST['company_address'] ) || ! wp_verify_nonce( $_POST['seller_fields'], basename(__FILE__) ) ) {
return $post_id;
}
$seller_meta['company_address'] = esc_textarea( $_POST['company_address'] );
foreach ( $seller_meta as $key => $value ) :
if ( 'revision' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action( 'save_post', 'wdbs_save_seller_meta', 1, 2 );

Adding metabox to custom post type category

I can add extra form elements to category editor using the following hook:
add_action ( 'edit_category_form_fields', 'my_custom_function');
What I wonder is, is there a way to add extra form elements to a "custom post type category"
I checked the hook reference, I found hooks for terms (e.g. edit_term_taxonomy) but not categories.
Thanks
This will add a field called 'TERM META TEXT' to your categories. I did take out the nonce but I really think it should go back in. Also, it's just better to have some sanitization vs. none. This example includes javascript and CSS hooks which you may or may not need but you can quickly see how all the parts go together.
// REGISTER TERM META
add_action( 'init', '___register_term_meta_text' );
function ___register_term_meta_text() {
register_meta( 'term', '__term_meta_text', '___sanitize_term_meta_text' );
}
// SANITIZE DATA
function ___sanitize_term_meta_text ( $value ) {
return sanitize_text_field ($value);
}
// GETTER (will be sanitized)
function ___get_term_meta_text( $term_id ) {
$value = get_term_meta( $term_id, '__term_meta_text', true );
$value = ___sanitize_term_meta_text( $value );
return $value;
}
// ADD FIELD TO CATEGORY TERM PAGE
add_action( 'category_add_form_fields', '___add_form_field_term_meta_text' );
function ___add_form_field_term_meta_text() { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<div class="form-field term-meta-text-wrap">
<label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label>
<input type="text" name="term_meta_text" id="term-meta-text" value="" class="term-meta-text-field" />
</div>
<?php }
// ADD FIELD TO CATEGORY EDIT PAGE
add_action( 'category_edit_form_fields', '___edit_form_field_term_meta_text' );
function ___edit_form_field_term_meta_text( $term ) {
$value = ___get_term_meta_text( $term->term_id );
if ( ! $value )
$value = ""; ?>
<tr class="form-field term-meta-text-wrap">
<th scope="row"><label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label></th>
<td>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<input type="text" name="term_meta_text" id="term-meta-text" value="<?php echo esc_attr( $value ); ?>" class="term-meta-text-field" />
</td>
</tr>
<?php }
// SAVE TERM META (on term edit & create)
add_action( 'edit_category', '___save_term_meta_text' );
add_action( 'create_category', '___save_term_meta_text' );
function ___save_term_meta_text( $term_id ) {
// verify the nonce --- remove if you don't care
if ( ! isset( $_POST['term_meta_text_nonce'] ) || ! wp_verify_nonce( $_POST['term_meta_text_nonce'], basename( __FILE__ ) ) )
return;
$old_value = ___get_term_meta_text( $term_id );
$new_value = isset( $_POST['term_meta_text'] ) ? ___sanitize_term_meta_text ( $_POST['term_meta_text'] ) : '';
if ( $old_value && '' === $new_value )
delete_term_meta( $term_id, '__term_meta_text' );
else if ( $old_value !== $new_value )
update_term_meta( $term_id, '__term_meta_text', $new_value );
}
// MODIFY COLUMNS (add our meta to the list)
add_filter( 'manage_edit-category_columns', '___edit_term_columns' );
function ___edit_term_columns( $columns ) {
$columns['__term_meta_text'] = __( 'TERM META TEXT', 'text_domain' );
return $columns;
}
// RENDER COLUMNS (render the meta data on a column)
add_filter( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 );
function ___manage_term_custom_column( $out, $column, $term_id ) {
if ( '__term_meta_text' === $column ) {
$value = ___get_term_meta_text( $term_id );
if ( ! $value )
$value = '';
$out = sprintf( '<span class="term-meta-text-block" style="" >%s</div>', esc_attr( $value ) );
}
return $out;
}
// ADD JAVASCRIPT & STYLES TO COLUMNS
add_action( 'admin_enqueue_scripts', '___admin_enqueue_scripts' );
function ___admin_enqueue_scripts( $hook_suffix ) {
if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy )
return;
// ADD YOUR SUPPORTING CSS / JS FILES HERE
// wp_enqueue_style( 'wp-color-picker' );
// wp_enqueue_script( 'wp-color-picker' );
add_action( 'admin_head', '___meta_term_text_print_styles' );
add_action( 'admin_footer', '___meta_term_text_print_scripts' );
}
// PRINT OUR CUSTOM STYLES
function ___meta_term_text_print_styles() { ?>
<style type="text/css">
.column-__term_meta_text { background-color:rgb(249, 249, 249); border: 1px solid lightgray;}
.column-__term_meta_text .term-meta-text-block { display: inline-block; color:darkturquoise; }
</style>
<?php }
// PRINT OUR CUSTOM SCRIPTS
function ___meta_term_text_print_scripts() { ?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$input_field = $( '.term-meta-text-field' );
// console.log($input_field); // your input field
} );
</script>
<?php }
Yes, you can, it is called taxonomy (the custom category). The hook you are looking for is:
$taxonomy_edit_form_fields
The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug (custom taxonomy you have registered. Like e.g. for care_types it will be car_types_edit_form_fields).
Source:
https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/
Note the hook you referred above is deprecated see this page:
This hook has been deprecated. Use {$taxonomy}_edit_form_fields
instead.
Source:
https://developer.wordpress.org/reference/hooks/edit_category_form_fields/

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

Custom Meta Box for Wordpress add a checkbox that inserts a small bit of CSS

I would like to add a simple meta box checkbox option to every post and page in a Wordpress site.
The text of the box:
'Check here to hide the slider featured text'
If the box is checked then it should insert the following CSS into the page:
.featured-text { display: none !important;}
Thanks for any and all help with this.
Here is what I'm working with:
The metabox is inserting correctly and saving, but the css is not being added
// This section is working...
// add meta checkbox to hide featured text on the slider
add_action( 'add_meta_boxes', 'add_hide_slider_text' );
function add_hide_slider_text()
{
add_meta_box( 'hide_slider_text_checkbox', 'Hide Slider Text', 'hide_slider_text_func', 'post', 'side', 'high' );
add_meta_box( 'hide_slider_text_checkbox', 'Hide Slider Text', 'hide_slider_text_func', 'page', 'side', 'high' );
}
function hide_slider_text_func( $post )
{
$values = get_post_custom( $post->ID );
$check = isset( $values['hide_slider_text_check'] ) ? esc_attr( $values['hide_slider_text_check'][0] ) : '';
wp_nonce_field( 'my_hide_slider_text_nonce', 'hide_text_nonce' );
?>
<p>
<input type="checkbox" name="hide_slider_text_check" id="hide_slider_text_check" <?php checked( $check, 'on' ); ?> />
<label for="hide_slider_text_check">Hide slider text overlay?</label>
</p>
<?php
}
add_action( 'save_post', 'hide_text_option_save' );
function hide_text_option_save( $post_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['hide_text_nonce'] ) || !wp_verify_nonce( $_POST['hide_text_nonce'], 'my_hide_slider_text_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Save only if it's checked, or delete it if it's not
if ( isset( $_POST['hide_slider_text_check'] ) && $_POST['hide_slider_text_check'] ) {
add_post_meta( $post_id, 'hide_slider_text_check', 'on', true );
} else {
delete_post_meta( $post_id, 'hide_slider_text_check' );
}
}
// this section isn't working at the moment
// now we insert the new css if the box is checked
if (get_post_meta($post->ID, 'hide_slider_text_check', true)) {
function my_styles_method() {
wp_enqueue_style(
'custom-style',
get_template_directory_uri() . '/style.css' //if style.css is loaded then insert the css
);
$custom_css = "
.featured-text { display: none !important;}";
wp_add_inline_style( 'custom-style', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'my_styles_method' );
}
Ok, I'd try with this (this kinda looks like what I'm doing when I'm fixing my themes :D)
function hide_slider_text_func( $post )
{
$values = get_post_custom( $post->ID );
$hide_slider_text_check = (isset( $values['hide_slider_text_check'] ) && $values['hide_slider_text_check'][0] == 1? 1 : 0;
wp_nonce_field( 'my_hide_slider_text_nonce', 'hide_text_nonce' );
?>
<p>
<label><input type="checkbox" name="hide_slider_text_check" id="hide_slider_text_check" value="1" <?php checked( $hide_slider_text_check, 1); ?> />Hide slider text overlay?</label>
</p>
<?php
}
add_action( 'save_post', 'hide_text_option_save' );
function hide_text_option_save( $post_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['hide_text_nonce'] ) || !wp_verify_nonce( $_POST['hide_text_nonce'], 'my_hide_slider_text_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
$hide_slider_text_check = (isset($_POST["hide_slider_text_check"])) ? $_POST["hide_slider_text_check"]==1 ? 1 : 0;
update_post_meta($post_id, "hide_slider_text_check", $hide_slider_text_check);
}
}
After that, depending on how you have defined your 'opt_name' value, you first define in your theme where you want it to execute, on the top of your php file global $opt_name and say $custom_data = get_post_custom(); then in front of your slider you can add
<?php if(isset($custom_data ['hide_slider_text_check'][0]) && $custom_data ['hide_slider_text_check'][0]==1): ?>
//your code goes here
<?php endif; ?>
If I haven't made any big errors, this should made the code in between the if part run only if you have checked the option for it. My saving metabox is a bit different so there could be a problem, but I don't think it should have to make any problems.

WordPress meta box doesn't save anything

I have more custom meta in my wordpress and one of them doesn't save anything.
This is the code for saving
add_action('save_post', 'save_details');
function save_details($post_id){
$slug = 'homepage';
/* check whether anything should be done */
$_POST += array("{$slug}_edit_nonce" => '');
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"], plugin_basename( __FILE__ ) ) ){
return;
}
/* Request passes all checks; update the post's metadata */
if (isset($_REQUEST['link_homepage'])) {
update_post_meta($post_id, 'link_homepage', $_REQUEST['link_homepage']);
}
}
Can anyone help me, please?
Thanks!
I have finally created a working code and after i write in the custom field in wordpress and click save removes it from textarea field but saves it and it works.
This is the new coode:
<?php
// add meta box for post types
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box(
'settings_meta_id',
'Link Homepage',
'settings_meta',
'homepage',
'normal',
'high'
);
}
function settings_meta( $post ){
//global $post;
$values = get_post_custom( $post->ID );
$links = isset( $values['link_homepage'] ) ? $values['link_homepage'][0] : '';
wp_nonce_field( 'homepage_box_nonce', 'link_homepage_nonce' );
?>
<p>
<label for="link_homepage">Link homepage:</label><br />
<textarea cols="100" rows="2" name="link_homepage" style="width:98%;"><?php echo $link_homepage; ?></textarea>
</p>
<?php }
add_action( 'save_post', 'save_custom_details' );
function save_custom_details( $post_id ) {
global $post;
//skip auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
//check for you post type only
if( $post->post_type == "homepage" ) {
if( isset($_POST['link_homepage']) ) { update_post_meta( $post->ID, 'link_homepage', $_POST['link_homepage'] );}
}
}
?>
How to fix that BUG, to keep the text there after saving?

Resources