TransWikia.com

code in functions.php not restricting or behaving as it should be

WordPress Development Asked by Nayan Chowdhury on March 9, 2021

I have written the below script to restrict users to access bbpress forums or topics. Here are the conditions:

  1. If user in not logged in will be redirected to “Join” page.
  2. If user is logged in but doesn’t have “pmpro_role_1” role, he will be redirected to “Product Page”.
  3. If a user is the administrator, he can view the page.

The problem here is for topics in forum it is working except the latest created topic. And it is taking user to “Join” page even if the user is logged in and have the “pmpro_role_1”. What is happening here?

Please help.

    add_action ('template_redirect', 'forum_security');
    
    function forum_security(){
        $roles = wp_get_current_user()-> roles;
        if(is_singular( array( 'forum', 'topic' ) )){
            if(!is_user_logged_in()){
                wp_redirect('/join');
                exit;
            }else{
                if(current_user_can('administrator')){
                    return;
                }elseif(!in_array('pmpro_role_1', $roles)){
                    wp_redirect('/product/channel-mcgilchrist');
                    exit;
                }
            }
            
        }
    }

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP