Troubles with saving metabox

时间:2015-12-16 作者:markb

所以我以前有一些代谢箱,它们的标签很差(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

1 个回复
最合适的回答,由SO网友:1991wp 整理而成

There are different ways of saving the metaboxes.
for e.g. you can create 2 files 
1) test-spec.php
2) test-meta.php
在test-spec.php文件中

<?php
//DEFINE VARIABLE TO STORE THE METABOX
$content_test_meta = new WPAlchemy_MetaBox(array
(
\'id\' => \'_content_test_meta\',   //UNIQUE ID FOR THIS META BOX
\'types\' => array(\'post\'), //LIMIT TO ONLY SHOW ON Default POST TYPE
\'template\' => get_stylesheet_directory() . \'/metaboxes/test-meta.php\'   //WHERE THE METABOX TEMPLATE IS FOR THIS METABOX
));
?>
在测试元中。php文件

假设我想保存文本字段

<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control metabox">

<label>Text Box</label>
<?php $metabox->the_field(\'text_box\'); /* SET THE FIELD ID */ ?>
        <p><input type="text" name="<?php $metabox->the_name(); /* SET THE INPUT NAME TO THE FIELD ID */ ?>" value="<?php $metabox->the_value(); /* SET THE INPUT VALUE TO THE FIELD VALUE */ ?>" class="wp-editor-area" /></p>

</div>

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x