Home  >  Article  >  CMS Tutorial  >  How to set up WordPress to allow users to read articles only after registering

How to set up WordPress to allow users to read articles only after registering

尚
Original
2019-07-20 09:47:064909browse

How to set up WordPress to allow users to read articles only after registering

In WordPress, you can set a separate password for each post and then send it to specific users through other methods. This function is more user-friendly, but it has no effect on user conversion. Advantages, but also relatively cumbersome. In fact, we can achieve this goal by setting custom columns of articles.
Suppose we set a custom column: Useronly. When there is a field set in this article, it means that this article is restricted to members.

add_filter('the_content', 'Useronly');
function Useronly($text){
    global $post;
    $Useronly = get_post_meta($post->ID, 'Useronly', true);
    if($Useronly){
        global $user_ID;
        if(!$user_ID){
            $redirect = get_permalink($post->ID);
            $text = '该内容仅限于会员浏览,请登录!';
        }
    }
    return $text;
}

Just add the above code to the theme’s functions.php file, or you can customize the prompt text and column name.
Then in the upper right corner of the WordPress backend, at the top, there is a "Display Options", click the triangular arrow to check the custom column.
Then enter the key in the custom column window when editing the article (key) is "Useronly" and value (value) is the constant "true".

For more wordpress related technical articles, please visit the wordpress tutorial column to learn!

The above is the detailed content of How to set up WordPress to allow users to read articles only after registering. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn