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

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

Related

WordPress redirect to page if taxonomy is empty

I have a taxonomy option called "tipo_coche", tipo_coche contains convertible, coupe, compacto, etc.. All created with CPT UI, some can be empty. I need an automatic reirect to page "/coches-segunda-mano-ocasion" when they do not contain elements.
I have tried with this code but without success.
function custom_redirector(){
if (isset($_GET["s"]) and $_GET["s"]=='' and !empty($_SERVER['HTTP_REFERER'])){
if (strpos($_SERVER['HTTP_REFERER'],'tipo_coche')!==false){
header("location: ".home_url('/coches-segunda-mano-ocasion')); exit;
}
}
}
You could take a look at the function: wp_safe_redirect() like so:
wp_safe_redirect( $url );
exit;

Wordpress: which function is called when user visit /?p=randomstring

Wordpress at the moment return a random page when visiting, for example, https://www.examplewebsite.com/?p=bdmvxmqa
At the moment it doesn't return a 404 and I'd like to correct it. But I cuouldn't find where to look into the code.
Wordpress usually redirects to the closest matching page title. (at least it used to way back when I was working with WordPress).
Try adding this line to functions.php to turn off this feature:
remove_filter('template_redirect', 'redirect_canonical');
If this is an old URL structure then you could redirect all ?p requests to your homepage or similar with the below function:
function rusty_redirect_query() {
global $wp;
$wp->add_query_var('p');
if(get_query_var('p')) {
wp_redirect( home_url( '/page/to/redirect/' ) );
}
exit();
}
add_action( 'template_redirect', 'rusty_redirect_query' );

Redirect wordpress user to another page based on assigned role

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

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

Hide page for logged in Users

I wanna do the following:
If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5. I tried adding the below code into my header, but it didn't work.
add_action( 'init', 'check_redirect_page' );
function check_redirect_page() {
if ( is_user_logged_in() && is_page( 10 ) ) {
wp_redirect( get_permalink( 5 ) );
exit;
}
}
Try using the wp hook rather than init; WordPress won't have got far enough at init to know whether it's dealing with a particular page or not.
add_action( 'wp', 'check_redirect_page' );
From the Conditional Tags documentation:
Warning: You can only use conditional query tags after the
posts_selection action hook in WordPress (the wp action hook is the
first one through which you can use these conditionals). For themes,
this means the conditional tag will never work properly if you are
using it in the body of functions.php, i.e. outside of a function.

Resources