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!