WordPress Tutorial

WordPress restricts non login users to browse classified articles

 Alibaba Cloud

WordPress The website has created multiple classified directories. By default, all classified directory articles are allowed to be viewed by all visitors. How can I achieve this if I want unlisted visitors to only access specified classified articles? WordPress does not provide this function in the background by default. You still need to use code or plug-in unit realization. The code shared below implements wordpress to restrict visitors from accessing specified classified articles.

Implementation code: In the current theme Add code to the functions.php file of

Want to be here? contact us bar
 Maker host
  1.  function limited access ( ) {
  2. 	 if ( ! is_user_logged_in ( )  &&  ! is_home ( )  && in_category ( array ( three , two ) ) {
  3.  wp_die ( 'No access rights' ) ;
  4. 		 exit ( ) ;
  5. 	 }
  6.  }
  7.  add_action (  'template_redirect' ,  'limited access' ,  zero  ) ;

Code description:

1. When visitors browse pages other than the home page, if the category ID of the category directory of the article they visit is 3 or 2, "No Access Permission" will appear. If you don't want to pop up a prompt, you can use the wp_redirect() function to jump;

2. The 3,2 in the in_category (array (3,2)) code is the classification directory ID

WordPress restricts non login users to browse classified articles

118 people have bought
View Demo Upgrade VIP Buy Now

Collection
fabulous ( zero )

Post reply

Hot selling template

Ashade - Works exhibition photo album WordPress Chinese theme
 LensNews

This site accepts WordPress/PbootCMS/DedeCMS, etc
System construction, imitation, development, customization and other services!