Redirect wordpress user to another page based on assigned role - wordpress

Does anyone know a way (or plugin) that would allow me to automatically redirect users to another page, based on their assigned user role, if they try accessing the main /wp-admin page?
The tricky part is (I guess), is that I still need them to be able to access sub-admin pages (ie. mysite.com/wp-admin/edit.php) -- it would only redirect them if they tried going to the main/dashboard page, mysite.com/wp-admin

I've recently had the problem where I needed to redirect multiple pages. The following code will check the roles you want to redirect ($valid-roles) and if not valid redirect to a given URL... in this case its /access-denied/.
Note: The page ID's in the array list of pages to re-direct.
add_action( 'template_redirect', 'role_based_redirect' );
function role_based_redirect() {
if( is_page( array( 1488, 2413, 2379, 2265, 2396, 2370, 2366, 4600 ) ) ) { //check the list of "corporate" pages
$user = wp_get_current_user();
$valid_roles = [ 'administrator', 'corporate', 'editor' ];
$the_roles = array_intersect( $valid_roles, $user->roles );
// The current user does not have any of the 'valid' roles.
if ( empty( $the_roles ) ) {
wp_redirect( home_url( '/access-denied/' ) );
exit;
}
}
}

There are many ways you can set the URL redirect to a different link by role on your WordPress page, you may use a plugin or you can also build a plugin to customize it specifically on your own way follow this link for that instruction but I believe as a second and better option this plugin right here might be easy for you might help you as well.
Good luck

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

Unlogged user cannot see store and must go to login and successfully logged in users must be redirected to store

I am working on a Wordpress site with Woocommerce. I am using this function inside functions.php to redirect everyone who visits my store to login since it is a private area. Once the person logs in, they should be redirected to the store again. I have the following code implemented, which works for me to get all those trying to navigate my store to log in, I need to modify this code so that once logged in, it redirects me to the store page in woocommerce, in my case the page of the store is called "tienda".
To clarify: Users who are not registered or logged in, should not be able to see the store or cart or anything from woocommerce, I already implemented this with a wp-members plugin, if they try to navigate these woocommerce urls, they must go to login. When you have already logged in, you should go to the store, in my case you should not redirect to the previous link, but must inevitably go to the page called "tienda".
On the other hand, I would like to know if my function is missing something or is well built. Thank you!
function loggedoutuser_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
header('Location: ' . wp_login_url());
exit;
}
}
add_action('template_redirect', 'loggedoutuser_redirect');
function woocommerce_login_redirect_custom( $redirect, $user ) {
$redirect = wc_get_page_permalink( 'shop' );
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'woocommerce_login_redirect_custom', 10, 2 );
Paste this above snippet at the end of your active child theme functions.php file or else if you are good in plugin development then create a custom plugin.
Snippet Explanation:
Create a custom function to redirect after logged in
Fetch the shop URL using WooCommerce helper function wc_get_page_permalink( 'shop' ) and assign it to the variable $redirect
Then return the URL
Hook the custom function to the filter hook woocommerce_login_redirect
Note: You can check the user role inside our custom function then based on the role you can redirect it to the dashboard if he is administrator else to the shop page.

How do I redirect user who adds "author/admin" to my domain name?

If you type "author/admin" after a wordpress website domain name (ie "mywebsite.com/author/admin") it will show you a list of all articles posted by the admin. I would like a user to be redirected to the homepage if they try to access this page.
I wanted to do redirect anyone who types in "2016" or "2017" as well. For example, if someone goes to my website and types in "mywebsite.com/2017" they would normally see a list of all articles from 2017 but I added this code to functions.php that now redirects them to the home page:
function redirect_to_home( $query ){
if(is_date() ) {
wp_redirect( home_url() );
exit;
}
} add_action( 'parse_query', 'redirect_to_home' );
If I change the "is_date" portion of that code to "is_author" it redirects the user anytime they type in "author/admin" but I can't figure out how to do both. I've tried adding two sets of code, one with "is_date" and one with "is_author" but I get a wordpress error when I try to save it. Is there a way to combine "is_date" and "is_author" into one set of code to redirect users in both cases?
You need || (or) comparison operator:
function redirect_to_home( $query ){
if(is_date() || is_author()) {
wp_redirect( home_url() );
exit;
}
} add_action( 'parse_query', 'redirect_to_home' );
Also have a look here for more comparison operators: https://www.w3schools.com/php/php_operators.asp

Hide wp-login.php as log-in and redirect it to a custom login page

I'm trying to hide the wp-login.php on my site so I installed the plugin Rename wp-login.php, I renamed it as log-in. Now I want to redirect it to my custom login page so the default login form of wordpress is totally hidden. Is there a way to accomplish this? I have already tried a redirection plugin and the code below however it only supports the wp-login.php and not my new login url:
function redirect_login_page(){
// Store for checking if this page equals wp-login.php
$page_viewed = basename( $_SERVER['REQUEST_URI'] );
// permalink to the custom login page
$login_page = get_permalink( '10' );
if( $page_viewed == "wp-login.php" ) {
wp_redirect( $login_page );
exit();
}
}
Is there a way to do this without actually altering the wordpress base files?
Try this
add_action('init','custom_login');
function custom_login(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
wp_redirect('http://localhost/wordpresstest/blog/');
exit();
}
}
Try This Out
// Hook the appropriate WordPress action
add_action('init', 'prevent_wp_login');
function prevent_wp_login() {
// WP tracks the current page - global the variable to access it
global $pagenow;
// Check if a $_GET['action'] is set, and if so, load it into $action variable
$action = (isset($_GET['action'])) ? $_GET['action'] : '';
// Check if we're on the login page, and ensure the action is not 'logout'
if( $pagenow == 'wp-login.php' && ( ! $action || ( $action && ! in_array($action, array('logout', 'lostpassword', 'rp', 'resetpass'))))) {
// Load the home page url
$page = get_bloginfo('url');
// Redirect to the home page
wp_redirect($page);
// Stop execution to prevent the page loading for any reason
exit();
}
}
You can achieve the results of accessing wordpress admin via custom admin login page using a plugin named All In One WP Security & Firewall.
The All In One WordPress Security plugin will take your website security to a whole new level. This plugin is designed and written by experts and is easy to use and understand.It reduces security risk by checking for vulnerabilities, and by implementing and enforcing the latest recommended WordPress security practices and techniques.
Your requirement:
This plugin has ability to hide admin login page. Rename your WordPress login page URL so that bots and hackers cannot access your real WordPress login URL. This feature allows you to change the default login page (wp-login.php) to something you configure.
For more features, refer plugin codex
you can refer this plugin https://wordpress.org/plugins/custom-login-url/
or
https://www.inkthemes.com/how-to-redirecting-wordpress-default-login-into-a-custom-login-page/

WooCommerce - Redirecting a User after They've Reset Their Password

I'm setting up a WooCommerce shop for a client and he has requested that the user be redirected back to the login form after the reset password form has been submitted.
Is this possible? Which function controls this?
Thanks in advance.
A much cleaner solution is to redirect using the woocommerce_customer_reset_password action:
function woocommerce_new_pass_redirect( $user ) {
wp_redirect( get_permalink(woocommerce_get_page_id('myaccount')));
exit;
}
add_action( 'woocommerce_customer_reset_password', 'woocommerce_new_pass_redirect' );
This code can be placed in functions.php.
Someone asked me about doing this and I think I'm going to talk them out of it, but here's what I came up with so that you could see the message and then be redirected in 5 seconds. Add a message/link to let them know you'll be redirecting. Plus, this will last through a WooCommerce upgrade if you add the template file to your theme
Add the form-lost-password.php template file to your theme and add the following code:
if ( $_GET[reset] = true && ( 'lost_password' == $args['form']) ) {
$my_account_url = get_site_url() . '/my-account';
echo "<script>window.setTimeout(function(){window.location.replace('$my_account_url')}, 5000)</script>";
}
If you dont want the time delay get rid of the window.setTimeout() and just use window.location.replace('$my_account_url').
I'm just facing the same question. I think the function that controls this is in the file woocommerce/includes/class-wc-form-handler.php. I changed the following line:
wp_redirect( add_query_arg( 'reset', 'true', remove_query_arg( array( 'key', 'login' ) ) ) );
with
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
With that change, you get redirected to the my-account page, where the customer can log in, but of course there are two problems doing that:
The customer gets no message the the password recovery was successful
Doing a WooCommerce update, the file gets overwritten with the original file
Would be great, if someone could come up with a "update-secure" solution.
Best regards
Christoph

Resources