How do I redirect this page URL, http://localhost/wordpress_rnd/?page_id=2
, to the home URL, http://localhost/wordpress_rnd/
, without using any plugins?
4 Answers
The correct way to do this is using the template_redirect
hook by adding a function to your functions.php
:
function redirect_to_home() {
if(!is_admin() && is_page('2')) {
wp_redirect(home_url());
exit();
}
}
add_action('template_redirect', 'redirect_to_home');
-
1could you tell me why do we have to use exit after wp_redirect(home_url()); ?– D.JCodeCommented Jan 5, 2021 at 16:22
-
1Good question and I can't say for sure, but I know that the official documentation example uses it, so I use it.– cfxCommented Jan 5, 2021 at 20:42
add_action( 'init', function() {
if ( 0 === stripos( $_SERVER['REQUEST_URI'], '/page_id=2' ) ) {
wp_redirect( home_url(), 301 );
exit;
}
}
Put this code in a mu-plugin or in your theme's functions.php file
Locate page.php (assuming you've created it already). After this line <?php get_header(); ?>
add the following code:
<?php if(is_page('2')) {
wp_redirect( home_url(), '302' );
} ?>
In the code above, is_page('2')
is actually the ID of your page as you've specified in your example.
-
This can do the job but I think it is better, depending on the exact situation, to do the redirection at server level, for example using .htaccess, so zero line of code is exectued and you don't waste resources.– cybmetaCommented Mar 14, 2015 at 9:09
-
@cybmeta valid point indeed :) Thanks for bringing it to my attention. Commented Mar 14, 2015 at 11:38
-
One more note. Use your code in an action hook, like
init
suggested by @paul in his answer, not in a template file as you suggested. If you switch to other theme, you loose the funtionality, that is why functinalities and look&feel (theme and template file) should be separated. I would edit the answer to add the action hook.– cybmetaCommented Mar 14, 2015 at 11:53 -
True as well. There's time one doesn't think about optimising the code but the focus is on getting the task at hand done faster :) This is was (in my opinion) one of those scenarios. I do, however, really appreciate your input and it's really valuable - everyday I learn something new in here :) Commented Mar 14, 2015 at 14:36
-
Write your code in an action hook is as fast as write it in a template file.– cybmetaCommented Mar 14, 2015 at 14:39
WP_REDIRECT is the function that you need to use for redirecting in wordpress. It can be used like :
wp_redirect( $location, $status );
exit;
//$location is required parameter. It is used to give the target url to which page will get redirected.
//$status is optional. It is used to set status code. Default is 302
You can use this function to redirect users from one page to other. It should be placed in either functions.php or the template file which is being used to display the current page. Now to use it in your situation, simple place the following code at the bottom of your functions.php file
$redirectFromPageID = 2; //Redirect from Page having ID of 2
$redirectTo = home_url(); //Redirect to Home URL
if( is_page( $redirectFromPageID ) ){
wp_redirect( $redirectTo );
exit;
}