我一直在研究这个问题,这个解决方案为“隐私”设置页面添加了一组新选项。
function oxide_setup_options() {
register_setting(\'oxide-privacy\', \'blog_open\');
$blog_open = get_option(\'blog_open\');
if ( empty( $blog_open ) ) {
add_option(\'blog_open\', \'0\');
}
}
add_action(\'admin_init\', \'oxide_setup_options\');
function oxide_restrict_toggle() { ?>
<?php settings_fields(\'oxide-privacy\'); ?>
<legend class="screen-reader-text"><span><?php _e( \'Site Access Restriction\' ); ?> </span></legend>
<input id="blog-open" type="radio" name="blog_open" value="1" <?php checked(\'1\', get_option(\'blog_open\')); ?> />
<label for="blog-open"><?php _e( \'Allow access to all users.\' );?></label><br/>
<input id="blog-closed" type="radio" name="blog_open" value="0" <?php checked(\'0\', get_option(\'blog_open\')); ?> />
<label for="blog-closed"><?php _e( \'Restrict access to logged in users.\' ); ?></label>
<p class="description"><?php _e( \'Note: This option blocks access to your site — like a boss.\' ); ?></p>
<?php do_settings_fields(\'oxide-privacy\', \'default\'); ?>
<?php }
add_action(\'blog_privacy_selector\', \'oxide_restrict_toggle\');
function oxide_restrict_access() {
if ( !get_option(\'blog_open\') && !is_user_logged_in() ) {
wp_redirect( wp_login_url() ); exit;
}
}
add_action(\'parse_request\', \'oxide_restrict_access\');
只需将这些代码放入主题函数中即可。php文件或创建一个包含代码的简单插件,您就可以开始了!