The east wind doesn't come
The catkins don't fly in March

WordPress article content is visible to VIP users Pure code implementation

I believe many people know that a WordPress plug-in is called erp hpdown, which has many functions. The most common one is to set the user's permission to view the content of an article. Because bloggers don't like to use plug-ins very much, they try to use code to implement what can be implemented in code, and they don't talk much about releasing code directly:

1. Add function core code

Copy the following code to functions. php in the theme file

 add_shortcode( 'vip_only', 'users_read_shortcode' ); function users_read_shortcode( $atts, $content = null ) { if ( is_user_logged_in() && ! empty( $content ) && current_user_can( 'read' ) )    {  return $content; } if ( !is_user_logged_in()){ Return 'Tips: You need to log in to view the content here!'; } if ( is_user_logged_in() && ! current_user_can( 'read' )){ Return 'Tips: You don't have permission to view the content here. Please contact the administrator to upgrade your user permissions!'; } }

After adding this code, you can use it directly in the text edit box

 [vip_only] Hidden content [/vip_only]

The article hidden content is visible to VIP users!
be careful: Please set Change to [

2. Add VIP user visible button

Of course, it is tedious to manually input these short codes each time, so the blogger here adds a "VIP user visible" button to the traditional WordPress editor, and the code is directly copied to the functions.php file. As for the students who use Gutenberg editor, there is no need to add it here, because the blogger does not bother with the editor, So how to add a custom button blogger is not clear!

 function appthemes_add_vip_only() { ?> <script type="text/javascript"> if ( typeof QTags != 'undefined' ) { QTags. addButton ('vip_only ','VIP user visible', '[vip_only]', '[/vip_only]'); }  </script> <? php  } add_action('admin_print_footer_scripts', 'appthemes_add_vip_only' );

be careful: Please set Change to [

3. Add user role

Because all the user roles of WordPress have read permission, we need to add a new user role. Without giving the role read permission, we can directly copy the following code to functions.php:

 //Add Normal User Role Add_role ('pt_user ',' normal user ', array( 'read '=>false,//read permission 'edit_posts'=>false,//Edit permission 'delete_posts'=>false,//Delete permissions ));

After adding this role, we can see a new role of "normal user" in the background, as shown in the following figure:

Finally, in the background of WordPress, select "Settings"==>"General"==>"Default Role for New Users" to set it as a normal user, as shown in the following figure:

This is the end of the tutorial! As for the display style, you can adjust it yourself!

Like( seven ) Reward
Copyright notice: This article is authorized by the Knowledge Sharing Attribution 4.0 International License Agreement [BY-NC-SA]
Article name: WordPress Article Content Settings Visible to VIP Users
Article link: https://www.wenzika.com/1478.html
The resources of this website are only for personal learning and exchange. Please delete them within 24 hours after downloading, and they are not allowed to be used for commercial purposes, otherwise the legal issues will be borne by yourself.

comment three

  • Nickname (required)
  • Email (required)
  • website
  1. #0

    This method removes the reading permission of all non VIP users. Does that mean that the newly published articles are invisible to subscribers and visitors regardless of whether the VIP is set to be visible? This function needs to be adjusted. Is it necessary to add judgment to the article template page to show different levels of visitors?

    uu0216 Three years ago (2021-07-02) Friends from China  Google Browser  Windows 10 reply
  2. #0

    If you are a member, you can see it. If you want to set it as a normal user, you can't see it only for VIP. Can you help me see where it is? Contact my email

    jaylon Four years ago (2020-09-02) Friends from China  Google Browser  Mac OS X 10_14_6 reply
    •  Wonder Zhou

      @ jaylon If you read the tutorial carefully, the read, write and delete permissions of ordinary users have been removed. According to the truth, ordinary users have no right to view as long as you add this function!

      Wonder Zhou Four years ago (2020-09-04) Friends from China  Google Browser  Windows 10 reply

Reward the author of the article if you think it is useful

Thank you very much for your reward, we will continue to give more high-quality content, let's create a better online world together!

Scan Alipay and reward

Scan WeChat and reward