admin – Want to be able to redirect readers from a wordpress site but still be able to access the back end

Question

You can do it with the “template_redirect” hook. For multisite or simple WordPress. e.g:

add_action('template_redirect', function(){

    $user = wp_get_current_user();
    $userRoles = $user->roles;

    $url = site_url();

    if(!in_array('administrator', $userRoles) && !is_front_page()){
        wp_redirect($url);
        exit;
    }

});

EDIT
is_front_page() will work only if “Front page displays” is set. Use is_home() if not.

0
David Borrink 2 weeks 2023-09-17T07:14:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse