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.
EDIT #2
Accordind with our discuss below, an example for when you play with multisite and you want redirect according to the current site:
add_action('template_redirect', function(){
$user = wp_get_current_user();
$userRoles = $user->roles;
$url = get_home_url(3);
if(!in_array('administrator', $userRoles) && get_current_blog_id() != 3){
wp_redirect($url);
exit;
}
});
Related Posts:
- Can’t login to WordPress admin panel after changing domain to use WordPress pages as a fallback from React
- Redirect Restricted Page to 404
- Redirect user to original url after login?
- Redirect to dashboard after login
- Make a video post redirect to next post after completion
- Redirect blog page to latest post?
- WordPress site redirects to different wordpress domain on the same server after reuploading db backup
- WordPress Redirect based on the prescence of a cookie
- Links to an exisiting Page are now redirecting to a draft page?
- Random and Erroneous WordPress Redirect
- Redirect Old domain & posts to new domain homepage
- Redirecting users to referrer page after logging in using custom login form
- Remove lost password form and URL
- add_rewrite_rule to pass entire path as a single parameter
- What is the wordpress way of displaying local external content?
- How To Only Allow Users To View Their Own Buddypress Profiles? [closed]
- Menu links only using http after enabling https, unable to redirect http links
- some url does not redirect from http to https
- Why can’t I access files / directories outside of wordpress
- Return to where the user was, after log out
- Change ‘Back To Website’ link in wp-login.php
- How do I map a subdomain in wordpress to the primary domain in a multisite setup?
- Redirect the non-www version of the site to the www
- Load time cost of using a redirect plugin v direct .htaccess entry
- Intercept the “lost password” action by first redirecting to an existing instructions page
- How to pass email values from one php page to another
- How to redirect after login getting a variable from url (for example with the language)
- How to set 301 redirection after moving WordPress blog?
- How to include error message on login
- How to use a custom domain?
- Changing siteurl and homeurl – internal links redirect to home page?
- WooCommerce unexpected behavior [closed]
- Force WordPress to load from site’s hostname
- redirect Login problems
- How can I force the user to log in, even if they’re already authenticated?
- 301 redirect instead of 404 when URL is a prefix of a post or page name?
- Redirect users after register (first login) and force submit post
- Some admin pages redirecting to front page on save
- How to pass a message using template_redirect
- .htaccess 301 redirect conflict with plugin “WordPress Https”
- Disable WordPress URL auto complete
- How do I put up a splash page and have all WordPress links redirect to this page?
- How can I disable author pages without 301 redirects?
- Redirect specific URL before other redirects take place in .htaccess?
- W3 Cache redirects to front page when I press purge all caches
- Stop forcing certain paths to end with a forward slash
- Redirection on Custom page
- How do I redirect users after submitting a topic for moderation?
- Direct iPhone visitors to different stylesheet?
- How to delete a post and keep its slug to refer to another post
- 301 redirect any RSS feed on level up url
- Redirect loop with similar URLs
- Why does WP HTTP API switch the method (POST/PURGE) to GET when redirecting (302)?
- Redirect user to specific link after login
- 301 redirect via htaccess rules on the new site
- WordPress Domain redirects to different host
- Change the domain url?
- After retrive password, redirect to index page
- Change Category Slug Redirection
- custom login form redirect problem
- WordPress redirect to new domain name
- Blog post is redirecting to home page
- Bulk redirect all post
- Disable redirect to homepage after successful Login, Stay on Current Page
- WordPress no permission redirection to a popup
- Open wordpress page in directory the way index.html would open
- How force WordPress redirect to current custom page after login
- Redirect Homepage to another page when user login
- Site using static homepage, redirects (301) and removes pluses from query string
- Unusual Apache2 Error Causing WordPress Redirect Error?
- Redirect error after updating WordPress to 5.4.2
- Determine user destination on wp-login.php
- 301 Redirect Loop www to non-www – Nginx + Apache2
- how to redirect 404 to attachment.php?
- 301 Redirect in WordPress not working in htaccess
- Toggle Redirect Function On/Off With Checkbox
- My wordpress site has “https://” on it but when I go to Dashboard and click visit site, it removes the “https://”
- After disabling Multisite, homepage redirects to non-existing page
- redirect to https://my-site/wp-admin/ instead of https://my-site/wordpress/wp-admin/ after options updating
- Redirect after login when WordPress in subdirectory
- All links redirecting to homepage
- How to redirect non-admin/editors from specific pages?
- How to make a redirect link in wordpress?
- wp_redirect leading to an infinite loop
- Redirect after Permalink change – What regex do I use?
- Configure WordPress to listen on a port other than 80
- Do I need to keep the original page when 301 redirecting to a new one?
- Redirect based on $_GET parameters
- Logging back into WordPress after logging out and having issues with ‘loggedout=true’ in redirect URL
- redirect users to the login page
- Moving an ASP Site to WordPress – Looking for Help on Redirects
- WordPress subfolders htaccess bypassing the root htaccess RewriteRule
- WordPress Redirecting Form Action to Home Page
- Redirect to Homepage if URL contains string
- How to redirect to a page after submitting form data?
- How to setup an automatic redirect for when there has been a critical error on the site?
- How to force HTTP and stop SSL completey on WordPress website
- How to Setup 2 Domains on a Single WordPress (Regions Filter)?
- WordPress keeps redirecting to root site after saving any setting
- Change Website Homepage from WordPress to Xenforo