如何限制登录用户只能查看某些页面?

时间:2020-07-22 作者:danielradst

如何通过wp\\u get\\u current\\u user()限制特定登录用户(当前角色为订阅方)仅查看特定页面?

1 个回复
最合适的回答,由SO网友:Fredrik 整理而成

此代码过滤自定义帖子类型的内容“;新闻“;,你说;“特定页面”;因此,这可以根据您的需要进行调整。然后它检查用户是否是订阅者,如果是,则显示内容,否则,它只会说;不允许;。

function filter_content($content) {

    global $post;
    if ( $post->post_type == \'news\' ) { //Specify your pages here
    
        $currentuser = wp_get_current_user();
        if ( in_array( \'subscriber\', (array) $currentuser->roles ) ) {
            return $content;
        } else {
            echo \'Not allowed\';
        }

    }

}

add_filter(\'the_content\', \'filter_content\');`

相关推荐