我认为目前为止,将答案与管理区域字段结合起来禁用作者存档是最好的选择。
一个封装所有内容的类(以及稍后将清除的一些常量和方法):
<?php
class Author_Archive_Disabler
{
// meta key that will store the disabled status
const KEY = \'_author_archive_disabled\';
// nonce name
const NONCE = \'author_archive_nonce\';
private static $ins = null;
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public static function init()
{
add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
}
// helper to see if the archive is disabled.
public static function is_disabled($user_id)
{
return \'on\' == get_user_meta($user_id, self::KEY, true);
}
// adds actions and such
public function _setup()
{
//
}
}
添加挂接到的字段
edit_user_profile
(在除您自己的配置文件以外的配置文件上显示字段)和
show_user_profile
(在您自己的配置文件上显示字段)。
<?php
class Author_Archive_Disabler
{
// snip snip
// adds actions and such
public function _setup()
{
add_action(\'edit_user_profile\', array($this, \'field\'));
add_action(\'show_user_profile\', array($this, \'field\'));
}
public function field($user)
{
// only let admins do this.
if(!current_user_can(\'manage_options\'))
return;
echo \'<h4>\', __(\'Disable Archive\', \'author-archive-disabler\'), \'</h4>\';
wp_nonce_field(self::NONCE . $user->ID, self::NONCE, false);
printf(
\'<label for="%1$s"><input type="checkbox" name="%1$s" id="%1$s" value="on" %2$s /> %3$s</label>\',
esc_attr(self::KEY),
checked(get_user_meta($user->ID, self::KEY, true), \'on\', false),
__(\'Disable Author Archive\', \'author-archive-disabler\')
);
}
}
非常简单:只向管理员显示字段、打印标题、nonce字段和“禁用”复选框本身。
你陷入了edit_user_profile_update
(其他人的个人资料)和personal_options_update
(您自己的个人资料)以保存内容。
<?php
class Author_Archive_Disabler
{
// snip snip
// adds actions and such
public function _setup()
{
add_action(\'edit_user_profile\', array($this, \'field\'));
add_action(\'show_user_profile\', array($this, \'field\'));
add_action(\'edit_user_profile_update\', array($this, \'save\'));
add_action(\'personal_options_update\', array($this, \'save\'));
}
// snip snip
public function save($user_id)
{
if(
!isset($_POST[self::NONCE]) ||
!wp_verify_nonce($_POST[self::NONCE], self::NONCE . $user_id)
) return; // nonce is no good, bail
if(!current_user_can(\'edit_user\', $user_id))
return; // current user can\'t edit this user, bail
update_user_meta($user_id, self::KEY,
!empty($_POST[self::KEY]) ? \'on\' : \'off\');
}
}
验证nonce,确保当前用户可以实际编辑用户,然后保存内容。如果选中该框,它将作为“on”进入。
现在连接到template_redirect
, 检查作者页面,如果禁用,则404。
<?php
class Author_Archive_Disabler
{
// snip snip
// adds actions and such
public function _setup()
{
add_action(\'edit_user_profile\', array($this, \'field\'));
add_action(\'show_user_profile\', array($this, \'field\'));
add_action(\'edit_user_profile_update\', array($this, \'save\'));
add_action(\'personal_options_update\', array($this, \'save\'));
add_action(\'template_redirect\', array($this, \'maybe_disable\'));
}
// snip snip
public function maybe_disable()
{
global $wp_query;
// not an author archive? bail.
if(!is_author())
return;
if(self::is_disabled(get_queried_object_id()))
{
$wp_query->set_404();
}
}
}
您还可以过滤作者链接,以便禁用的用户永远不会链接到。
<?php
class Author_Archive_Disabler
{
// snip snip
// adds actions and such
public function _setup()
{
add_action(\'edit_user_profile\', array($this, \'field\'));
add_action(\'show_user_profile\', array($this, \'field\'));
add_action(\'edit_user_profile_update\', array($this, \'save\'));
add_action(\'personal_options_update\', array($this, \'save\'));
add_action(\'template_redirect\', array($this, \'maybe_disable\'));
add_filter(\'author_link\', array($this, \'change_link\'), 10, 2);
}
// snip snip
public function change_link($link, $author_id)
{
if(self::is_disabled($author_id))
return apply_filters(\'author_archive_disabler_default_url\', home_url(), $author_id);
return $link;
}
}
以上所有内容
as a plugin