Can't get meta values to save

时间:2017-07-04 作者:Mandu

我正在尝试编写一个类来制作元框。这些框显示得很好,但我似乎无法获取要保存的值。似乎根本没有调用Save函数。

作为进一步的实验,我手动向数据库添加了一个元值,它在编辑器中显示得很好,但当我单击“更新”时,条目被删除了。

如有任何建议,将不胜感激。这是我的密码。

    /**
 * Reusable custom meta boxes class
 *
 * Reusable custom meta boxes
 * Part of Cloudsmith WP-Framework used to help create custom post types for Wordpress.
 *
 * @author  Cloudsmith Studio
 * @link    https://cloudsmith.ca
 * @version 1.0
 * @license 
 */

class Cloudsmith_Meta_Box {

    /**
     * Meta Box ID
     *
     * @var string $box_id Holds id of the custom meta box to be created.
     */
    public $box_id;

    /**
     * Meta Box Title
     *
     * @var string $box_title Holds the title of the meta box
     */
    public $box_title;

    /**
     * Meta Box Content
     *
     * @var string $box_context Holds the context parameter for the meta box
     */
    public $box_context;

    public $box_priority;

    public $box_post_type;

    /**
     * Meta Box Content
     *
     * @var array $box_meat_fields Holds an array of arrays for the fileds to be displayed in a meta box
     */
    public $box_meta_fields;


    public static function create( $title, $fields = array(), $post_type, $context = \'normal\', $priority = \'high\' ){

        // If the Parameters are Empty, Do nothing
        if( ! empty ( $title ) ){

            // Instanitate a new object for the meta box
            ${$title . \'_meta_box\'} = new Cloudsmith_Meta_Box;


            // Assign the properties
            ${$title . \'_meta_box\'}->box_id          = Cloudsmith_Utility::uglify( $title ) . \'_meta_box\';
            ${$title . \'_meta_box\'}->box_title       = Cloudsmith_Utility::prettify( $title );
            ${$title . \'_meta_box\'}->box_context     = $context;
            ${$title . \'_meta_box\'}->box_priority    = $priority;
            ${$title . \'_meta_box\'}->box_post_type   = $post_type;
            ${$title . \'_meta_box\'}->box_meta_fields = $fields;

            $meta_box = ${$title . \'_meta_box\'};
            add_action( \'admin_init\', 
                function() use( $meta_box ) {

                    add_meta_box(
                        $meta_box->box_id ,
                        $meta_box->box_title,
                        [ $meta_box, \'display_meta_box\' ],
                        $meta_box->box_post_type,
                        $meta_box->box_context,
                        $meta_box->box_priority,
                        $meta_box->box_meta_fields
                    );
                    add_action(\'save_post\', [ $meta_box, \'save_meta_value\' ] );
                }
            );

        }
     } // End of Create Method

     public function display_meta_box(){


        global $post;

        // use nonce for input verification
        echo \'<input type="hidden" name="\' . $this->box_id . \'_meta_box_nonce" value="\'.wp_create_nonce( basename( __FILE__ ) ).\'" />\';

        // loop through $fields array to create form inputs.
        echo \'<table class="form-table">\';
        foreach ( $this->box_meta_fields as $field ){

            // Get value for this field if it exists
            $meta_value = get_post_meta( $post->ID, $field[\'id\'], true );

            // Begin table row
            echo
                \'<tr>
                    <th><label for="\' . $field[\'id\'] . \'">\' . $field[\'label\'] . \'</label></th>
                    <td>\';

                        // display HTML based on the type of input
                        switch( $field[\'type\'] ){

                            // Text input
                            case \'text\':
                                echo \'<input type="text" name="\' . $field[\'id\'] . \'" id="\' . $field[\'id\'] . \'" value="\'. $meta_value . \'" size="30" />\';
                                echo \'</br><span class="description">\' . $field[\'desc\'] . \'</span>\';
                            break;

                            // Text Area Input
                            case \'textarea\':
                                echo \'<textarea name="\' . $field[\'id\'] . \'" id="\' . $field[\'id\'] . \'" cols="60" rows="4">\'. $meta_value . \'</textarea>\';
                                echo \'</br><span class="description">\' . $field[\'desc\'] . \'</span>\';
                            break;

                            // Select Box input
                            case \'select\':
                                echo \'<select>\';
                                while ( list( $key, $value ) = each( $field[\'options\'] ) ){
                                    echo \'<option \', $meta_value == $value ? \'selected="selected"\' : \'\', \'value="\' . $value .\'">\' . $key . \'</option>\';
                                }
                                echo \'</select><br>\';
                                echo \'<span class ="description">\' . $field[\'desc\'] . \'</span>\';
                            break;

                            // Checkbox input

                            case \'checkbox\':
                                echo \'<input type="checkbox" name="\'. $field[\'id\'] .\'" id="\' . $field[\'id\'] .\'"\', $meta_value ? \'checked="checked"\' : \'\',\'/>\';
                                echo \'<label for"\' . $field[\'id\'] . \'">\' . $field[\'desc\'] . \'</label>\';
                            break;

                            // Single Select Radio Box input
                            case \'radio\':

                            break;

                        } // End of switch
                    echo 
                    \'</td>
                </tr>\';
        } // End foreach

        echo \'</table>\'; // end of input table
     }

     public function save_meta_value( $post_id ) {

        global $post;

        echo \'<div class="notice updated"><p>Saved!!!??</p></div>\';
        //var_dump($this->box_meta_fields);

        // verify nonce
        if (!wp_verify_nonce($_POST[$this->box_id . \'_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;
        }

        // loop through fields and save the data
        foreach ($this->box_meta_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);
            }
        } // end foreach
    }
} // End of Meta Box Class
然后我在这里调用我的方法:

    // Create a new custome post type for quizzes.
$quizzes = new Cloudsmith_Custom_Post_Type( \'Quiz\', \'Quizzes\', $args );

// Add taxonomies to the quiz post type
Cloudsmith_Taxonomy::create( \'quiz_category\', \'quiz\', \'Category\', \'Categories\', [\'hierarchical\' => true] );
Cloudsmith_Taxonomy::create( \'quiz_tags\', \'quiz\', \'Tag\', \'Tags\' );
Cloudsmith_Taxonomy::create( \'quiz_difficulty\', \'quiz\', \'Difficulty Level\', \'Difficulty Levels\', 
    // custom taxonomy settings
    [
        \'hierarchical\' => true,
        \'show_in_menu\' => false
    ] 
);

Cloudsmith_Meta_Box::create( \'Questions\',
    [
        [
            \'label\' => \'First Question\',
            \'desc\'  => \'What is the question you want to ask?\',
            \'id\'    => "Question One",
            \'type\'  => \'text\'
        ],
        [
            \'label\' => \'Second Question\',
            \'desc\'  => \'Do you have another question?\',
            \'id\'    => \'Question Two\',
            \'type\'  => \'checkbox\'
        ],
        [
            \'label\' => \'Third Question\',
            \'desc\'  => \'How many more?\',
            \'id\'    => \'Question Three\',
            \'type\'  => \'textarea\'
        ],
        [
            \'label\' => \'Fourth Question\',
            \'desc\'  => \'At least 2\',
            \'id\'    => \'Question 4\',
            \'type\'  => \'select\',
            \'options\' => 
                [
                    \'option 1\' => \'one\',
                    \'option 2\' => \'two\',
                    \'option 3\' => \'three\'
                ]
        ]
    ],
    \'quiz\'
);

Cloudsmith_Meta_Box::create( \'Answers\',
    [
        [
            \'label\' => \'First Answer\',
            \'desc\'  => \'Do Two boxes work?\',
            \'id\'    => "Answer One",
            \'type\'  => \'text\'
        ]
    ],
    \'quiz\'
);

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

找到了答案:

字段ID中有一个空格,而$\\u POST键会将其折叠。因此,在我的save函数的foreach循环中,ID不匹配。

我通过在元字段的ID上使用清理功能解决了这个问题。

我更加困惑,因为我插入的帮助我调试的管理员通知没有显示出来。我不知道在保存帖子后,页面会重定向,并且用户不会看到管理通知。因此,我认为save\\u post操作根本没有启动,而实际上是。

结束

相关推荐

在导航设置中添加自定义菜单项Metabox

我一直在设想一种功能,可以扩展WordPress拖放菜单的创建。本质上是添加一个自定义菜单项按钮,用于添加不同类型的项目,而不仅仅是链接。我设想一个基本用法只是一个文本字段,但未来的应用程序可能包括在菜单项中添加小部件区域。Visual Mockup of Idea:如果有任何信息和资源可以帮助我找到可能的解决方案,我将不胜感激。