php - 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 中的投资组合页面的代码。知道如何使它工作并保存数据吗?
谢谢!
解决方案
您的保存方法看起来不对。尝试类似的东西
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"]);
}
推荐阅读
- reactjs - NextJS - 使用 Webpack Externals 更快地构建
- android - 你如何在 android studio avd 中激活文本到语音
- java - 我想使用 recyclerview 将我的画廊加载到片段中。但它不起作用
- python - 如何为 Django 过滤子对象的父字段
- java - Java 13 迁移后无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
- javascript - firefox 阻止 ctrl + shift + p 快捷方式打开私人浏览器窗口
- php - 将 if else 语句的“echo”结果保存到变量中?
- c++ - 我可以在不同的重定向上混合 new 和 malloc 吗?
- android - 为什么我无法在 android.R.style.mycustomstyle 的主要活动中使用我的自定义样式?
- c# - 将 xamarin 形式的文件作为 src 文件夹读取