如何向特定注册用户显示类别帖子

时间:2012-09-22 作者:miguelalas

我在一个私人博客上工作,我想知道是否可以用任何其他插件或任何其他方式实现这一点:假设我们有3个类别:CA、CB和CC,以及3个用户,UA、UB和UC,所有用户都可以发布,但只能在特定类别中发布,如CA中的UA等。现在,当其中一个用户登录查看博客时,这将只显示允许该用户使用的帖子类别。有什么建议吗?

PD:Allow Categories插件应该可以做到这一点,但它在wp 3.4.2中根本不起作用,很久以前也没有更新过。

2 个回复
SO网友:chrisguitarguy

这是一个很棒的插件材料。这个答案还将包括如何将用户与类别相关联(如果不知道这一点,很难给出解决方案)。

在我们开始之前,真正的肉是在第四步(在底部)。如果您不需要将类别与用户关联的方法,请跳到这一步。

所以首先,让我们把所有的事情都总结在一节课上。我们班有一个静电init 方法,该方法添加操作和(如)以及一些类常量。

<?php
WPSE65959_User_One_Cat::init();

class WPSE65959_User_One_Cat
{
    /**
     * Nonce name.
     *
     */
    const NONCE = \'wpse65959_nonce\';

    /**
     * The user meta key we\'ll use
     *
     */
    const KEY = \'wpse65959_category\';

    /**
     * The taxonomy we\'ll use.  Just \'category\' in this case.
     *
     */
    const TAX = \'category\';

    public static function init()
    {
        // add actions here
    }
}
第一步:在用户编辑页面中添加一个字段edit_user_profile 仅在编辑其他用户配置文件时显示。您在编辑自己的配置文件时永远不会看到它,但在编辑其他用户时会看到它。我们的字段将抓取当前所有类别,并将它们放在下拉菜单中。我们还将输出一个nonce字段,稍后在保存用户时检查该字段。

<?php
class WPSE65959_User_One_Cat
{
    // snip snip

    public static function init()
    {
        add_action(
            \'edit_user_profile\',
            array(__CLASS__, \'show_field\')
        );
    }

    public static function show_field($user)
    {
        $terms = get_terms(self::TAX, array(\'hide_empty\' => false));

        wp_nonce_field(self::NONCE . $user->ID, self::NONCE, false);

        echo \'<h4>\';
        esc_html_e(\'User Category\', \'wpse65959\');
        echo \'</h4>\';

        printf(\'<select name="%1$s" id="%1$s">\', esc_attr(self::KEY));
        echo \'<option value="">----</option>\';
        foreach($terms as $t)
        {
            printf(
                \'<option value="%1$s" %2$s>%3$s</option>\',
                esc_attr($t->term_id),
                selected(get_user_meta($user->ID, self::KEY, true), $t->term_id, false),
                esc_html($t->name)
            );
        }
        echo \'</select>\';
    }
}
这一领域的另一面是我们需要拯救它。这样做,钩住edit_user_profile_update. 该函数将验证nonce,并确保当前用户具有编辑该用户的权限。

<?php
class WPSE65959_User_One_Cat
{
    // snip snip

    public static function init()
    {
        // snip snip

        add_action(
            \'edit_user_profile_update\',
            array(__CLASS__, \'save\')
        );
    }


    public static function save($user_id)
    {
        if(
            !isset($_POST[self::NONCE]) ||
            !wp_verify_nonce($_POST[self::NONCE], self::NONCE . $user_id)
        ) return;

        if(!current_user_can(\'edit_user\', $user_id))
            return;

        if(isset($_POST[self::KEY]) && $_POST[self::KEY])
            update_user_meta($user_id, self::KEY, absint($_POST[self::KEY]));
        else
            delete_user_meta($user_id, self::KEY);
    }
}
我们现在有了一种将用户与类别关联的方法。

第二步:移除类别元框add_meta_boxes_post. 如果用户不是管理员,请删除类别框,以便作者无法更改类别。

<?php
class WPSE65959_User_One_Cat
{
    // snip snip

    public static function init()
    {
        // snip snip

        add_action(
            \'add_meta_boxes_post\',
            array(__CLASS__, \'remove_metabox\')
        );
    }

    public static function remove_metabox()
    {
        if(current_user_can(\'manage_options\'))
            return; // this is an admin.  Admins can do what they want.

        remove_meta_box(
            \'categorydiv\',
            \'post\',
            \'side\'
        );
    }
}
第三步:更改非管理员的默认类别如果用户不是管理员,他们将看不到类别元框,因此我们需要确保他们的帖子属于正确的类别。钩入pre_option_default_category 要做到这一点。

<?php
class WPSE65959_User_One_Cat
{
    // snip snip

    public static function init()
    {
        // snip snip

        add_filter(
            \'pre_option_default_category\',
            array(__CLASS__, \'default_cat\')
        );
    }

    public static function default_cat($false)
    {
        if(current_user_can(\'manage_options\'))
            return $false; // don\'t change default category for admins

        if($cat = get_user_meta(wp_get_current_user()->ID, self::KEY, true))
            return $cat; // we have a default category for this user.

        return $false; // no default category, return the original value
    }
}
第四步:通过将用户与类别相关联的方式更改查询,我们现在可以更改查询以仅显示其类别中的帖子。

钩入request. 检查用户是否是管理员,如果不是,则更改查询以仅包括其类别。

<?php
class WPSE65959_User_One_Cat
{
    // snip snip

    public static function init()
    {
        // snip snip

        add_filter(
            \'request\',
            array(__CLASS__, \'request\')
        );
    }

    public static function request($vars)
    {
        if(current_user_can(\'manage_options\'))
            return $vars; // admins can view whatever

        // if the user has a default category, make sure they only see that category
        if($cat = get_user_meta(wp_get_current_user()->ID, self::KEY, true))
            $vars[\'cat\'] = $cat;

        // handle not having a category associated with them here

        return $vars;
    }
}
以上所有内容as a plugin

SO网友:kaiser

使用WP板上功能和»Template Hierarchy«.

根据您的需要,您可以针对single.phpcategory-*.php 模板。

工具箱:需要的数据首先,我们需要wp_get_current_user(). 然后,我们可以进行多次检查,具体取决于您要检查的内容。有关详细信息,请执行var_dump( wp_get_current_user() ); 并比较您的用户,以查看哪些数据可用,哪些数据可以与可靠的数据进行核对。提示:用户ID和user_login 始终是必需的,无法更改。

锁定效果并重新路由

然后,您只需在模板顶部、调用的正下方进行检查get_header(); 例如

// redirect all users, except the one with the ID of 5 to the index.php page
if ( 5 != wp_get_current_user()->ID )
{
    wp_redirect( home_url( \'/\' ) );
    exit;
}
你也可以将他重定向到他的作者页面,并在那里列出他所有允许的类别。

更改查询另一个选项是在通过检查用户ID从DB获取帖子之前拦截查询-请参阅详细信息in other questions using posts_clauses-filter.

针对不同用户的不同模板template_redirect 胡克,就像解释的那样in this answer.

结束

相关推荐

Query post & loop problem.

我在向循环添加查询时遇到问题。请看看有什么问题。here is the link <div class=\"fl\" id=\"main_article\"> <?php $count = 1; $the_query = new WP_Query( \'category_name=headline&orderby=post_date&order=desc\' ); if ( $the_query->have_posts(