在我试图找出当前问题的解决方案时,我的头撞墙。简而言之,我创建了一个'page-home.php'模板页面,可以获取一个元框以调用页面模板,但尝试使用元框中的数据更新页面会使数据消失。下面不保存数据的页面模板的自定义元框
代码:
function page_add_meta_boxes($post) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta($post->ID, '_wp_page_template', true);
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php')
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action('add_meta_boxes_page', 'page_add_meta_boxes');
function page_template_metabox() {
wp_nonce_field(basename(__FILE__), 'page_meta_box_nonce');
$some_string = get_post_meta($post->ID, '_some_string', true);
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if (!isset($_POST['page_meta_box_nonce']) || !wp_verify_nonce($_POST['page_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_page', $post_id)){
return;
}
// save our string
if (isset($_REQUEST['some-string'])) {
update_post_meta($post_id, '_some_string', sanitize_text_field($_POST['some-string']));
}
}
add_action('publish_page', 'page_save_custom_post_meta');
add_action('draft_page', 'page_save_custom_post_meta');
add_action('future_page', 'page_save_custom_post_meta');
任何建议都理解的。
尝试用这种ADD_ACTION(“save_post”,“page_save_custom_post_meta”); –
谢谢Akshay!不幸的是,即使在添加“保存帖子”操作后,它仍然不会保存。我不太确定是否有'save_page'动作,但我不相信。 – Leroy
您可以将您的输入类型名称some-string更改为somestring,并请相应地在请求区域中进行更改。我不确定这个建议 –