所以我以前有一些代谢箱,它们的标签很差(meta_1
, meta_2
) 我开始对它们进行重命名,以便维护时更容易理解。
当我回去查看其他用户是如何开发元数据库时,我发现了这种创建方式:
// Review field array
$prefix_review = \'meta_review_\';
$meta_review_information_array = array(
array(
\'label\' => \'Single checkbox\',
\'id\' => $prefix_review . \'checkbox\',
\'class\' => \'\',
\'type\' => \'checkbox\'
),
array(
\'label\'=> \'Number input\',
\'desc\' => \'\',
\'id\' => $prefix_review . \'number\',
\'class\' => \'\',
\'min\' => \'1900\',
\'max\' => \'2100\',
\'step\' => \'1\',
\'type\' => \'number\'
),
array(
\'label\' => \'Text input\',
\'desc\' => \'\',
\'id\' => $prefix_review . \'text\',
\'class\' => \'\',
\'type\' => \'text\'
),
array (
\'label\' => \'Checkbox group\',
\'desc\' => \'\',
\'id\' => $prefix_review . \'checkboxes\',
\'class\' => \'\',
\'type\' => \'checkbox_group\',
\'options\' => array (
\'one\' => array ( \'label\' => \'Label one\', \'value\' => \'one\' ),
\'two\' => array ( \'label\' => \'Label two\', \'value\' => \'two\' ),
\'three\' => array ( \'label\' => \'Label three\', \'value\' => \'three\' )
)
),
array (
\'label\' => \'Dropdown\',
\'desc\' => \'\',
\'id\' => $prefix_review . \'dropdown\',
\'class\' => \'\',
\'type\' => \'select\',
\'options\' => array (
\'one\' => array ( \'label\' => \'Label one\', \'value\' => \'one\' ),
\'two\' => array ( \'label\' => \'Label two\', \'value\' => \'two\' )
)
),
)
// Project field array
$prefix_project = \'meta_project_\';
$meta_project_information_array = array(
array(
\'label\' => \'Single checkbox\',
\'id\' => $prefix_project . \'checkbox\',
\'class\' => \'\',
\'type\' => \'checkbox\'
),
array(
\'label\'=> \'Number input\',
\'desc\' => \'\',
\'id\' => $prefix_project . \'number\',
\'class\' => \'\',
\'min\' => \'1900\',
\'max\' => \'2100\',
\'step\' => \'1\',
\'type\' => \'number\'
),
array(
\'label\' => \'Text input\',
\'desc\' => \'\',
\'id\' => $prefix_project . \'text\',
\'class\' => \'\',
\'type\' => \'text\'
),
array (
\'label\' => \'Checkbox group\',
\'desc\' => \'\',
\'id\' => $prefix_project . \'checkboxes\',
\'class\' => \'\',
\'type\' => \'checkbox_group\',
\'options\' => array (
\'one\' => array ( \'label\' => \'Label one\', \'value\' => \'one\' ),
\'two\' => array ( \'label\' => \'Label two\', \'value\' => \'two\' ),
\'three\' => array ( \'label\' => \'Label three\', \'value\' => \'three\' )
)
),
array (
\'label\' => \'Dropdown\',
\'desc\' => \'\',
\'id\' => $prefix_project . \'dropdown\',
\'class\' => \'\',
\'type\' => \'select\',
\'options\' => array (
\'one\' => array ( \'label\' => \'Label one\', \'value\' => \'one\' ),
\'two\' => array ( \'label\' => \'Label two\', \'value\' => \'two\' )
)
),
)
并且,通过一个
$switch[\'type\']
然后循环数组类(例如。
$meta_project_information_array
).
然而,当我去拯救它时,我被难住了。
首先,我会:
add_action(\'save_post\', \'_save_metabox\');
// Save the Data
function _save_metabox( $post_id ) {
global $meta_review_information_array, $meta_project_information_array;
// 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;
}
// Loop and save : review
foreach( $meta_review_information_array as $meta_review_info ) {
$meta_review_info_old = get_post_meta($post_id, $meta_review_info[\'id\'], true);
$meta_review_info_new = $_POST[$field_1[\'id\']];
if( $meta_review_info_new && $meta_review_info_new != $meta_review_info_old ) {
update_post_meta( $post_id, $meta_review_info[\'id\'], $meta_review_info_new );
} elseif( \'\' == $meta_review_info_new && $meta_review_info_old ) {
delete_post_meta($post_id, $meta_review_info[\'id\'], $meta_review_info_old);
}
}
// Loop and save: project
foreach( $meta_project_information_array as $meta_project_info ) {
$meta_project_info_old = get_post_meta($post_id, $meta_project_info[\'id\'], true);
$meta_project_info_new = $_POST[$field_1[\'id\']];
if( $meta_project_info_new && $meta_project_info_new != $meta_project_info_old ) {
update_post_meta( $post_id, $meta_project_info[\'id\'], $meta_project_info_new );
} elseif( \'\' == $meta_project_info_new && $meta_project_info_old ) {
delete_post_meta($post_id, $meta_project_info[\'id\'], $meta_project_info_old);
}
}
}
我想我的问题是:1。为什么我输入数据时上面的内容不保存?2、如何将nonce添加到保存部分?每个元盒都有单独的nonce还是都一样?一些CPT有多个元盒。有没有更好的办法?
目前我有3个php文件:
admin_column.php //this is the admin columns in the CPT
metabox.php //which has all the $types for the many metaboxes and the <table>
save_metabox.php //which is the saving of the metabox