我想做一些事情,比如如果我在管理中选中/取消选中复选框,然后在页脚的前面隐藏/显示所有社交链接。
function add_custom_meta_box()
{add_meta_box("demo-meta-box", "Add or remove social link from footer", "custom_meta_box_markup", "page", "advanced", "high", null); }
add_action("add_meta_boxes", "add_custom_meta_box");
/* Displaying Fields in a Custom Meta Box */
function custom_meta_box_markup($object)
{
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
<div class="meta_chk">
<?php
$checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
if($checkbox_value == "")
{
?>
<input name="meta-box-checkbox" type="checkbox" value="true">
<?php
}
else if($checkbox_value == "true")
{
?>
<input name="meta-box-checkbox" type="checkbox" value="true" checked>
<?php
}
?>
<label for="meta-box-checkbox">Remove social link</label>
</div>
<?php
}
/* Storing Meta Data */
function save_custom_meta_box($post_id, $post, $update)
{
if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta- box-nonce"], basename(__FILE__)))
return $post_id;
if(!current_user_can("edit_post", $post_id))
return $post_id;
if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
return $post_id;
$slug = "post";
if($slug != $post->post_type)
return $post_id;
$meta_box_checkbox_value = "";
if(isset($_POST["meta-box-checkbox"]))
{
$meta_box_checkbox_value = $_POST["meta-box-checkbox"];
}
update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}
add_action("save_post", "save_custom_meta_box", 10, 3);
SO网友:Krzysztof Grabania
它不能在post metabox中,因为它是站点设置,而不是特定页面。您可以通过以下方式在管理面板中为设置创建子菜单位置add_submenu_page
函数,例如:
add_action(\'admin_menu\', \'add_my_settings_submenu_page\');
function add_my_settings_submenu_page() {
add_submenu_page(\'options-general.php\', \'Page title\', \'Menu title\', \'manage_options\', \'my_settings\', \'my_settings_submenu_callback\');
}
然后,您需要为最后一个参数创建回调,其中将是代码的所有逻辑。应该是这样的:
<?php function my_settings_submenu_callback() {
if (isset($_POST[\'disable_footer_social\'])) {
update_option(\'disabled_footer_social\', $_POST[\'disable_footer_social\']);
}
$option = (bool) get_option(\'disabled_footer_social\', false);
?>
<div class="wrap">
<h1>My Settings</h1>
<form method="post">
<table class="form-table">
<th>Disable footer social buttons</th>
<td>
<input type="hidden" name="disable_footer_social" value="0">
<label>
<input type="checkbox" name="disable_footer_social" value="1" <?php echo $option ? \'checked="checked"\' : \'\'; ?>>
Disable
</label>
</td>
</table>
<?php submit_button(\'Submit\'); ?>
</form>
</div>
</table>
<?php }
在页脚中,您可以检查此选项是否由
get_option(\'disabled_footer_social\')
, 例如:
if (get_option(\'disabled_footer_social\', false) != 1) {
// show your buttons
}