首页 > 解决方案 > WordPress Metabox 不会保存

问题描述

我试图让我的元框工作,但出于某种原因,每当我输入一些文本并尝试将数据保存在文本区域内时,它都不会保存。

add_action("admin_init", "custom_product_metabox");

function custom_product_metabox(){
  add_meta_box("custom_product_metabox_01", "Product Description", "custom_product_metabox_field", "portfolio_page", "normal", "low");
}

function custom_product_metabox_field(){
    global $page;

    $data = get_post_custom($page->ID);
    $val = isset($data['custom_product_input']) ? esc_attr($data['custom_product_input'][0]) : 'no value';
    echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}

add_action("save_post", "save_detail");

function save_detail(){
    global $page;

    if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
        return $page->ID;
    }

    update_post_meta($page->ID, "custom_product_input", $_POST["custom_product_input"]);
}

这实际上是我嵌入到 functions.php 中的投资组合页面的代码。知道如何使它工作并保存数据吗?

谢谢!

标签: phpwordpressmeta

解决方案


您的保存方法看起来不对。尝试类似的东西

function custom_product_metabox_field($post){
    //global $page; remove this line also

    $data = get_post_custom($post->ID);
    $val = !empty(get_post_meta( $post->ID, 'custom_product_input', true )) ? 
   get_post_meta( $post->ID, 'custom_product_input', true ) : 'no value';
    echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
} 

add_action("save_post", "save_detail", 10, 3 );

function save_detail($post_id, $post, $update){
   //global $page;// remove this line

   if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
       return $post_id;
   }

   update_post_meta($post_id, "custom_product_input", $_POST["custom_product_input"]);
}

推荐阅读