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

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' );

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 Rewriting Rules

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?
Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

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

"login" in permalink redirects to wp-login.php wp3.6

I'm trying to create a custom login page for a custom user page, but I can't get wordpress to stop redirecting the word login to wp-login.php.
Setup is pretty simple, I'm adding a rule that looks like this. account/login/?$ that yields to index.php?account=login. I also setup my rewrite tag for that custom query var. I have a couple other rules setup and they all work properly.
When I use monkey man's rewrite analyzer everything points properly, but on the front end any time I enter login into the url it redirects me to wp-login.php. It doesn't matter what segment it is entered at. so /login, /account/login, /dogs/bulldogs/login.... all redirect to wp-login.php
As far as I can tell there aren't any other rewrite rules containing login. So i'm at a loss.
As a last piece of info, i'm working in wp 3.6
Any advise or ideas would be greatly appreciated.
I'm facing the exact same problem. Google wasn't being very helpful, so I had the need to dig in. :)
Right on top of the wp-includes/template-loader.php file, you find this little snippet of code:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES )
do_action('template_redirect');
If you inspect what functions are hooked to that action at that time, you'll find a reference to the wp_redirect_admin_locations function (both the hook and the function are declared on the /wp-includes/canonical.php file - line 526).
Within the wp_redirect_admin_locations function, you'll find this:
$logins = array(
home_url( 'wp-login.php', 'relative' ),
home_url( 'login', 'relative' ),
site_url( 'login', 'relative' ),
);
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
wp_redirect( site_url( 'wp-login.php', 'login' ) );
exit;
}
An easy solution for the problem, would be to hook a function of yours to the *wp_redirect* filter. Something like this (within your functions.php):
add_filter('wp_redirect', 'yourprefix_filter_login_redirect', 10, 2);
function yourprefix_filter_login_redirect($location, $status)
{
$uri = trim($_SERVER['REQUEST_URI'], '/');
if ($uri === 'login') {
return false;
}
return $location;
}
So... The *wp_redirect_admin_locations* terminates the script after trying to preform the redirect, so the solution above won't work.
The only other solution I'm seeing right now is to bypass the function all together which kind of sucks. All the other redirects defined within that function, will be lost all together...
Anyway, what I've ended up using:
add_action(
'init',
function() {
remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
}
);
Building on top of Diogos great answer, you can even disable the /login redirect without losing the other redirects. I just posted this solution on WPSE: Disable "/login" redirect

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

Resources