我想禁用某些用户的作者页,例如管理员用户不发布任何内容,但仍有作者页。
这可能吗?可能是一个插件,它在管理面板的用户页面中添加了一个选项来启用/禁用作者页面?
我想禁用某些用户的作者页,例如管理员用户不发布任何内容,但仍有作者页。
这可能吗?可能是一个插件,它在管理面板的用户页面中添加了一个选项来启用/禁用作者页面?
我认为目前为止,将答案与管理区域字段结合起来禁用作者存档是最好的选择。
一个封装所有内容的类(以及稍后将清除的一些常量和方法):
<?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我建议过滤author_link
, 过滤返回的输出get_author_posts_url()
:
$link = apply_filters(\'author_link\', $link, $author_id, $author_nicename);
像这样过滤:function wpse74924_filter_author_link( $link, $author_id, $author_nicename ) {
if ( user_can( $author_id, \'administrator\' ) ) {
// This is an administrator
return \'\';
// You could also return home_url() to link to the site front page
}
// Otherwise, return $link unmodified
return $link;
}
add_filter( \'author_link\', \'wpse74924_filter_author_link\' );
或者更干净的过滤器the_author_posts_link
, 过滤返回的输出the_author_posts_link()`:echo apply_filters( \'the_author_posts_link\', $link );
例如,您不想为显示作者存档索引administrator
用户,您可以这样做:function wpse74924_filter_the_author_posts_link( $link ) {
// Since $author_id doesn\'t get passed to this filter,
// we need to query it ourselves
$author_id = get_the_author_meta( \'ID\' );
if ( user_can( $author_id, \'administrator\' ) ) {
// This is an administrator
__return_false();
}
// Otherwise, return $link unmodified
return $link;
}
add_filter( \'the_author_posts_link\', \'wpse74924_filter_the_author_posts_link\' );
按照其他答案中的建议修改作者链接当然是解决方案的一部分。如果不使用这些链接,则不希望打印这些链接。如果你想确保杀死作者页面。。。
function tst() {
global $wp_query;
if (is_author() && !empty($wp_query->query[\'author_name\'])) {
$author = get_user_by(\'login\',$wp_query->query[\'author_name\']);
if ( 1 === $author->ID) {
wp_safe_redirect(get_bloginfo(\'url\'),\'301\'); // or whatever you want
}
}
}
add_action(\'template_redirect\',\'tst\');
您可以在template\\u重定向挂钩上检测作者名称并重定向到主页
http://codex.wordpress.org/Author_Templates#Setting_Up_for_Author_Information
我正在使用postTabs plugin 和Comprehensive Google Map Plugin, 我的问题是,当我在第二个选项卡上有我的谷歌地图时,地图没有按预期加载。但是如果我把它移到第一个标签上,效果会非常好。。有没有办法让地图在第二个选项卡上工作?实际上,无论我使用哪个选项卡插件,地图都不会正确加载到第二个选项卡上。。欢迎提出任何建议。谢谢:)