我正在尝试编写一个类来制作元框。这些框显示得很好,但我似乎无法获取要保存的值。似乎根本没有调用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\'
);