Php 在cpt中保存自定义元框数据未正确处理
设置 我编写了一段代码,其中创建了一个自定义帖子类型和一个元框,其中包含我的自定义帖子类型portfolio的传统信息。此元框包含3个字段,链接字段、日期字段和专业知识字段,它们与该自定义帖子类型一起存储和保存 问题 当我更新我的公文包项目时(没有错误),整个设置完全可以正常工作,但只要我开始在其他帖子类型中添加内容,例如页面、消息和我定义的其他帖子类型。我发现到处都有错误和通知,因为它试图将元框分配给这些帖子。虽然我认为我只将Meta框指定给名为portfolio的post类型,但我感到震惊的是,我在某处犯了一个错误。请参阅代码 因此,当我加载一个不同的posttype时,我会注意到它有一个未定义的索引Php 在cpt中保存自定义元框数据未正确处理,php,wordpress,Php,Wordpress,设置 我编写了一段代码,其中创建了一个自定义帖子类型和一个元框,其中包含我的自定义帖子类型portfolio的传统信息。此元框包含3个字段,链接字段、日期字段和专业知识字段,它们与该自定义帖子类型一起存储和保存 问题 当我更新我的公文包项目时(没有错误),整个设置完全可以正常工作,但只要我开始在其他帖子类型中添加内容,例如页面、消息和我定义的其他帖子类型。我发现到处都有错误和通知,因为它试图将元框分配给这些帖子。虽然我认为我只将Meta框指定给名为portfolio的post类型,但我感到震惊的
注意:未定义索引:第161行/home/jellyf1q/public\u html/test/webfanatics/wp/wp content/thethewebfanatics/cpt/portfolio.php中的dbt\u website-link
然而,当我保存它时,我在一次中得到了很多错误。除了通知外,我还收到了发送到标题错误的信息
Notice: Undefined index: dbt_website-link in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Notice: Undefined index: dbt_opleverdatum in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Notice: Undefined index: dbt_text in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-admin/post.php on line 197
Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-includes/pluggable.php on line 1228
假设
经过一点推演,我认为这与试图应用于所有帖子类型的元框有关。我还不知道如何解决这个问题,因为我已经在代码中告诉它,在我按照上一节的说明操作时,只使用post类型的公文包。它实际上遵循了该指令,因为自定义元框仅应用于公文包CPT,但是代码中发生了一些事情,使得它寻找元框字段的post变量的索引
代码
我在下面为metabox提供了代码。正如您在第5行的metabox数组中看到的,我告诉它分配给post类型的公文包
$prefix = 'dbt_';
$meta_box = array(
'id' => 'meta-portfolio',
'title' => 'Overige informatie',
'page' => 'portfolio',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Website link',
'desc' => 'De link naar het portfolio item bij onze klant',
'id' => $prefix . 'website-link',
'type' => 'text',
'std' => 'http://'
),
array(
'name' => 'Opleverdatum',
'desc' => 'De opleverdatum van het project',
'id' => $prefix . 'opleverdatum',
'type' => 'text',
'std' => 'dd/mm/yyyy'
),
array(
'name' => 'Expertise',
'desc' => 'De Expertise die is gebruikt binnen het project',
'id' => $prefix . 'text',
'type' => 'text',
'std' => 'Expertise'
)
)
);
add_action('admin_menu', 'portfolio_add_box');
// Add meta box
function portfolio_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'portfolio_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function portfolio_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="portfolio_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '</td><td>',
'</td></tr>';
}
echo '</table>';
}
add_action('save_post', 'portfolio_save_data');
// Save data from meta box
function portfolio_save_data($post_id) {
global $meta_box;
// verify nonce
if (isset($_POST['portfolio_meta_box_nonce']) && !wp_verify_nonce($_POST['portfolio_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
?>
第161行是指元框中的这段代码
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
这个钩子add_操作('save_post','portfolio_save_data')
用于保存所有帖子、页面和自定义帖子类型,而不仅仅用于CPT
因此,例如,当您添加新的常规post时,此函数也会运行,但没有元盒之类的东西,有时甚至没有$\u post
在功能公文包\u保存\u数据
检查权限之前,您可以执行以下检查:
if ( 'portfolio' !== get_post_type() ) {
return $post_id;
}
所以您的保存只在您的CPT中运行,否则让WP来完成它的工作
正如我上面提到的,有时$\u post中没有此类键'post\u type'
,因此您可以更改此行以检查权限:
if ( isset($_POST['post_type']) && 'page' == $_POST['post_type'])
最后一件事,根据Codex,当你用portfolio\u add\u box
注册你的元盒时,最好勾入add\u box这个钩子添加动作('save\u post','portfolio\u save\u data')
用于保存所有帖子、页面和自定义帖子类型,而不仅仅用于CPT
因此,例如,当您添加新的常规post时,此函数也会运行,但没有元盒之类的东西,有时甚至没有$\u post
在功能公文包\u保存\u数据
检查权限之前,您可以执行以下检查:
if ( 'portfolio' !== get_post_type() ) {
return $post_id;
}
所以您的保存只在您的CPT中运行,否则让WP来完成它的工作
正如我上面提到的,有时$\u post中没有此类键'post\u type'
,因此您可以更改此行以检查权限:
if ( isset($_POST['post_type']) && 'page' == $_POST['post_type'])
最后一件事,根据Codex,当你用portfolio\u add\u box
注册你的元盒时,最好加入add\u meta\u box
,太棒了,谢谢你的解释。也谢谢你给我的钩子小费。我在某个地方也读过这篇文章,但我只是从一个教程中学习,以便更好地理解这个过程。我很高兴我能够提供帮助:)太棒了,谢谢你的解释。也谢谢你给我的钩子小费。我也在某个地方读到了这篇文章,但我只是从一个教程中学习,以便更好地理解这些过程。我很高兴我能够提供帮助:)