admin管理员组文章数量:1395785
I have a problem. My fields are showing in the meta box, but saving does not work:
<?php
// Create the metabox and fields
function food_meta_box_markup($object) {
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
<div>
<label for="np-calories" class="np-label"><?php _e('Energy (kcal)', 'nutriplus') ?></label>
<input name="np_calories" type="number" class="np-field"
value="<?php echo get_post_meta($object->ID, "np_calories", true); ?>">
</div>
<?php
}
function add_food_meta_box() {
add_meta_box("food-meta-box", __('Nutritional Information', 'nutriplus'), "food_meta_box_markup", "np-food", "side", "high", NULL);
}
add_action("add_meta_boxes", "add_food_meta_box");
// Save the metabox and fields
function food_save_meta_box_data($post_id) {
// verify taxonomies meta box nonce
if (!isset($_POST['food_meta_box_nonce']) || !wp_verify_nonce($_POST['food_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;
}
if ($post->post_type == "np-food") {
// store custom fields values
// energy kcal string
if (isset($_REQUEST['np_calories'])) {
update_post_meta($post_id, 'np_calories', sanitize_text_field($_POST['np_calories']));
}
}
}
add_action('save_post_food', 'food_save_meta_box_data');
?>
My Metabox values don´t save :(
Regards
Buddy
I have a problem. My fields are showing in the meta box, but saving does not work:
<?php
// Create the metabox and fields
function food_meta_box_markup($object) {
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
<div>
<label for="np-calories" class="np-label"><?php _e('Energy (kcal)', 'nutriplus') ?></label>
<input name="np_calories" type="number" class="np-field"
value="<?php echo get_post_meta($object->ID, "np_calories", true); ?>">
</div>
<?php
}
function add_food_meta_box() {
add_meta_box("food-meta-box", __('Nutritional Information', 'nutriplus'), "food_meta_box_markup", "np-food", "side", "high", NULL);
}
add_action("add_meta_boxes", "add_food_meta_box");
// Save the metabox and fields
function food_save_meta_box_data($post_id) {
// verify taxonomies meta box nonce
if (!isset($_POST['food_meta_box_nonce']) || !wp_verify_nonce($_POST['food_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;
}
if ($post->post_type == "np-food") {
// store custom fields values
// energy kcal string
if (isset($_REQUEST['np_calories'])) {
update_post_meta($post_id, 'np_calories', sanitize_text_field($_POST['np_calories']));
}
}
}
add_action('save_post_food', 'food_save_meta_box_data');
?>
My Metabox values don´t save :(
Regards
Buddy
Share Improve this question edited Feb 13, 2020 at 16:29 mrben522 1,6931 gold badge12 silver badges17 bronze badges asked Feb 13, 2020 at 14:22 BuddyHoliBuddyHoli 56 bronze badges1 Answer
Reset to default 0This is my working code
// Add meta box
function frontpage_meta_boxes( $post ){
global $post;
if(!empty($post))
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-home.php' )
{
add_meta_box( 'frontpage_meta_box', __( 'Features' ), 'frontpage_meta_box', 'page', 'advanced', 'high' );
}
}
}
add_action( 'add_meta_boxes_page', 'frontpage_meta_boxes' );
// builds our meta box
function frontpage_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'frontpage_meta_box_nonce' );
// retrieve the _manuf_url current value
$manufacturer_url = get_post_meta( $post->ID, '_manuf_url', true );
?>
<h3><?php _e( 'Manufacturer URL' ); ?></h3>
<p>
<input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" />
</p>
<?php
}
// saves our data
function frontpage_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['frontpage_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['frontpage_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;
}
// manufacturer url string
if ( isset( $_REQUEST['manufacturer-url'] ) ) {
update_post_meta( $post_id, '_manuf_url', sanitize_text_field( $_POST['manufacturer-url'] ) );
}
// store custom fields values
}
add_action( 'save_post_page', 'frontpage_save_meta_box_data' );
本文标签: Metabox not saving values
版权声明:本文标题:Metabox not saving values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744750752a2623160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论