我试图在编辑器中为页面添加一个复选框,保存然后打印。下面的代码确实添加了复选框,但它没有保存,即单击更新按钮后,复选框仍保持未选中状态。
function register_post_assets(){
add_meta_box(\'featured-page\', __(\'Featured Page\'), \'add_featured_meta_box\', \'page\', \'advanced\', \'high\');
}
add_action(\'admin_init\', \'register_post_assets\', 1);
function add_featured_meta_box($post){
$featured = get_post_meta($post->ID, \'_featured-page\', true);
echo "<label for=\'_featured-page\'>".__(\'Feature this ppage\')."</label>";
echo "<input type=\'checkbox\' name=\'_featured-page\' id=\'featured-product\' value=\'1\' ".checked(1, $featured)." />";
}
function save_featured_meta($post_id){
if(isset($_REQUEST[\'featured-page\']))
update_post_meta(esc_attr($post_id, \'_featured-page\', esc_attr($_REQUEST[\'featured-page\']));
}
add_action(\'save_post\', \'save_featured_meta\');
SO网友:Ahmed Fouad
为什么要使用$\\u请求[\'featured-page\']?您的输入名称是\\u featured-page,您应该使用$\\u POST而不是$\\u REQUEST。
试试这样的。
add_action("admin_init", "register_post_assets");
function register_post_assets(){
add_meta_box(\'featured-page\', __(\'Featured Page\'), \'add_featured_meta_box\', \'page\', \'advanced\', \'high\');
}
function add_featured_meta_box( $post ){
global $post;
$custom = get_post_custom($post->ID);
$_featured-page = $custom["_featured-page"][0];
?>
<label>Featured Page</label>
<?php
$is_featured = get_post_meta($post->ID, \'_featured-page\', true);
$checked = ($is_featured == "yes") ? \'checked="checked"\' : null; ?>
<input type="checkbox" name="_featured-page" value="yes" <?php echo $checked; ?> />
<?php
}
add_action(\'save_post\', \'save_featured_meta\');
function save_featured_meta( $post_id ){
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
update_post_meta( $post_id, "_featured-page", $_POST["_featured-page"]);
}