WordPress Metabox 无法保存

问题描述 投票:0回答:1

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

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 中的投资组合页面的代码。 知道如何让它工作并保存数据吗?

谢谢!

php wordpress meta-boxes
1个回答
0
投票

您的保存方法看起来有误。尝试类似的事情

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"]);
}
© www.soinside.com 2019 - 2024. All rights reserved.