WordPress only allows visitors to view articles of specified categories

A child shoe asked how to realize that non logged in users can only browse specified classified articles. Here is the method.

 WordPress only allows visitors to view articles of specified categories

Only visitors are allowed to browse articles of the specified category

Similar codes can be found online:

 //The template_redirect action hook is bound to be executed, so this hook is valid for the whole station add_action( 'template_redirect', 'ashuwp_show_only_login', 0 ); function ashuwp_show_only_login(){ //Judge the login, and the about page is allowed to access if( ! is_page('about') && ! is_user_logged_in() ){ auth_redirect(); // Jump to login page exit(); } }

Code source: www.ashuwp.com

The original code only allows non login users to visit the specified page or directly jump to login.

Make a simple change and add a judgment in_category , only archive pages and articles of the specified category are allowed to be accessed.

 //Home page and designated classified articles can be accessed add_action( 'template_redirect', 'ashuwp_show_only_login', 0 ); function ashuwp_show_only_login(){ //Judge the login. Only classified articles with ID 3 and 2 are allowed to be accessed if( ! in_category( array( 3,2 ) ) && ! is_home() && ! is_user_logged_in() ){ auth_redirect(); // Jump to login page exit(); } }

By default, non login users are only allowed to visit the homepage of the website and classified archive pages and articles with classified IDs 3 and 2. Otherwise, they will jump to the login page.

hold ! in_category The exclamation mark in front is removed and changed to in_category On the contrary, access to classified articles with classification IDs 3 and 2 will jump to login, and other articles can be accessed normally.

The code is added to the topic function template functions.php.

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
WeChat copied
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments fifteen    Visitors twelve    Author three
    •  MaxhyTV
      MaxhyTV one

      Thanks, just needed!

      •  Hairiness
        Hairiness one

        Great, thanks for sharing. How can I reverse the selection? It means that only when you access this category, you can jump to login

          •  Robin
            Robin

            @ Hairiness Remove the exclamation mark in front of the judgment classification function

              •  Hairiness
                Hairiness one

                @ Robin It's perfect, thanks a million...

            •  Sansheng Stone Trusteeship
              Sansheng Stone Trusteeship two

              When tourists click "Designated categories or articles only allowed to be viewed by tourists", a prompt or page similar to "Please log in first to view" will be displayed, and then tourists can choose whether to log in or not. If visitors click Login, they will go to the login page. If visitors do not log in, they will return to the previous page. Is there a better user experience.

              •  Polk City
                Polk City two

                This function is very practical. I collected it

                •  Moscow
                  Moscow three

                  Boss, have you added short code to the content of the article to achieve plug-ins with different group permissions to see different content?

                  •  Source code network
                    Source code network zero

                    It feels so simple... try it

                    •  Purple cloud
                      Purple cloud one

                      It's very practical. I collected it.

                      •  Live up to this life
                        Live up to this life one

                        I learned.

                        •  Sign
                          Sign zero

                          Which year's articles are included in the categories that need to be logged in to access, which will lead to that year's site map also needs to be logged in to open, so the search engine cannot parse this site map

                          •  Modest study
                            Modest study one

                            Whether there is a way to directly designate individual articles that cannot be accessed by tourists. Tourists can directly visit the 404 page of the designated article link, and it will not be displayed on the home page. Normal access and display on the home page can only be achieved after logging in

                            add_action( ‘template_redirect’, ‘ashuwp_show_only_login’, 0 );
                            function ashuwp_show_only_login(){
                            //Judge the login. Tourists are not allowed to access articles with category IDs of 1 and 2
                            if( in_category( array( 1,2 ) ) && ! is_home() && ! is_user_logged_in() ){
                            global $wp_query;
                            $wp_query->set_404();
                            status_header(404);
                            nocache_headers();
                            include( get_query_template( ‘404’ ) );
                            die();
                            exit();
                            }
                            }
                            My method can only classify ID and tourists can't access it, but the article link can't be displayed on the home page. This is based on the code of Brother Bird

                              •  Robin
                                Robin

                                @ Modest study First page exclusion classification

                                function exclude_category_home( $query ) {
                                if ( $query->is_home ) {
                                $query->set( \'cat\', \'-1, -2, -3\' );
                                }
                                return $query;
                                }

                                add_filter( \'pre_get_posts\', \'exclude_category_home\' );

                                  •  Modest study
                                    Modest study one

                                    @ Robin Can I change it to the specified article ID (not excluding classification)? Although I have written it for me with ChatGPT, I also added search engine judgment. Ha ha, tourists can't access the specified article, visit 404, search engine access is normal, login users can access and view normally, and ChatGPT is really strong

                               anonymous

                              Comment

                              Anonymous netizens
                               :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                              Drag the slider to complete validation