Wordpress Homepage Redirect to other page - wordpress

SO I have this problem when I go to my main website, for example, www.mysite.com I want to redirect it to www.mysite.com/home I'm using this plugin 'link. it works but not always, I don't understand why so then I wrote a little script in the header file
$GetURL = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
if ($GetURL === 'www.mywebsite.com') {
header('Location: www.mywebsite.com/home');
}
But still, it doesn't redirect. what could be a problem?

You can set your page as Front Page is Setting -> Reading or
add_action('template_redirect', 'function(){
if(is_front_page()){
wp_redirect(get_permalink('your_page_id'), 301)
}
});

Maxim's answer helped me redirecting the homepage to a custom post type archive, but it contains two typos. First, there must not be an apostrophe before "function"; second, the wp_redirect command needs to be closed with a semicolon.
This is a version of the code which, put into functions.php, points the front page to the archive of the post type 'project':
add_action(
'template_redirect',
function( $post_type ){
$post_type = 'project';
if ( is_front_page() ){
wp_redirect( get_post_type_archive_link( $post_type ) );
}
}
);
Obviously, you can use get_permalink() to redirect to a static page as well.

Related

How to redirect a user not logged in to a different page in wordpress

I would like to redirect a user that is not logged in to the page I created to log in. I wrote the code in the functions.php of the theme but it seems like that it doesn't working.
<?php
if (is_page($page = 'private-gallery' )) {
if (!is_user_logged_in() ) {
template_redirect ('http://www.site.it/wp/private-area' );
exit;
}
}
?>
You can do this by hooking into the template_redirect [action reference]. (note that you can also use an earlier executed hook, but I mostly use this for ease).
This way you can retrieve the current post slug, and based on this do your statement comparison to see if the current page is private-gallery.
In the below method the wp_redirect() [function reference] is used to redirect to the correct page. The home_url() [function reference] is used to retrieve the home URL for the current site (always try to avoid use full path URL's whenever possible).
The wp_redirect basically executes header("Location: $location", true, $status); where $status is 302 by default .
add_action('template_redirect', 'gianni_private_gallery_redirect');
function gianni_private_gallery_redirect(){
global $post;
if($post){
if( $post->post_name=='private-gallery' ) {
if ( !is_user_logged_in() ) {
// By default a 302 redirect.
// Add your custom status code as second parameter if required.
wp_redirect( home_url( '/private-area/' ) );
exit;
}
}
}
}

How to Stop Accessing WordPress Page When User Login

I am trying to stop users from accessing a page when they already login. Anyone please help me how to do this. I checked it on google and all over the stake overflow but did not find any solution. Hope to get answer soon.
Thanks
try this code bellow , put it within your theme function.
function my_page_template_redirect()
{
if( is_page( 'page-slug' ) && is_user_logged_in() )
{
wp_redirect( home_url() );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Change page slug based on your case, you also can use page_id check these references bellow. Don't forget to save permalink after put that code
Reference :
https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
https://developer.wordpress.org/reference/functions/is_page/

Wordpress add_rewrite_rule redirects if page is startpage

I do a custom wordpress rewrite with the following code:
function add_rewrite_rules() {
add_rewrite_rule(
'^mypath/([A-z0-9\-\_\,]+)/?$',
'index.php?page_id=2&tags=$matches[0]',
'top'
);
}
add_action('init', 'add_rewrite_rules');
This works fine:
When I open /mypath/tag1,tag2,tag3/ the page with the page_id==2 is shown, and GET parameter tags contains tag1,tag2,tag3.
The path stays /mypath/tag1,tag2,tag3/
But there is one exception:
When the page with the page_id==2 is marked as the wordpress startpage, then he forgets everything and redirects to /
Wordpress seems to redirect to WP_HOME, if the page is the startpage, but I want it to stay on /mypath/tag1,tag2,tag3/, because I want to load my startpage and use the tags parameter in an angular script.
Does somebody have an idea how I can prevent this redirect?
Found the solution by myself in this thread:
https://wordpress.stackexchange.com/questions/184163/how-to-prevent-the-default-home-rewrite-to-a-static-page
Just disable canonical redirect for front page:
function disable_canonical_redirect_for_front_page( $redirect ) {
if ( is_page() && $front_page = get_option( 'page_on_front' ) ) {
if ( is_page( $front_page ) )
$redirect = false;
}
return $redirect;
}
add_filter( 'redirect_canonical', 'disable_canonical_redirect_for_front_page' );

How to change search query string to permalink in Wordpress?

I'm having a problem with my Wordpress site when using search. This is probably due to qTranslate plugin.
So I have permalink turned on and when I use search field, I get URL with ?s= query string like this:
www.mydomain.com/en/?s=test
The above URL works just fine but if I have many search results that show pagination buttons, links on those buttons cause 404 errors. Link for second page has the following URL:
www.mydomain.com/en/page/2/?s=test
This throws 404 error but if I manually modify the URL like this:
www.mydomain.com/en/search/test/page/2/
then the pagination works.
So I would like to force Wordpress to use /search/ permalink instead of /?s= query string but not sure how to do that.
you can do it by the following function, paste this into your theme functions.php
function change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite' );
then the search url will be like search/test instead of ?s=test,this function is quit simple

wordpress URL end with a number

I am not sure if this is a known issue or suppose to be like this performance,
any wordpress website, if you put a number at the end of the URL, for example,
http://perishablepress.com/wordpress-multisite-mamp/
we put 1 or any numbers at the end
http://perishablepress.com/wordpress-multisite-mamp/123
We will not get the NOT FOUND page, we'll still stay at the same page, which is
http://perishablepress.com/wordpress-multisite-mamp/
Is this an error or normal ? how do we redirect to NOT FOUND PAGE instead?
It's not an error, the 123 is interpreted as a pagination parameter:
Request: wordpress-multisite-mamp/123
Query String: page=%2F123&name=wordpress-multisite-mamp
Matched Rewrite Rule: ([^/]+)(/[0-9]+)?/?$
Matched Rewrite Query: name=wordpress-multisite-mamp&page=%2F123
Posts can be paginated with <!--nextpage-->. WordPress displays the content of the last page if no further content is found (or the full post if the post is not paginated).
To redirect to the 404 page when the pagination parameter exeeds the number of pages, drop the following in your functions.php file:
add_action( 'template_redirect', 'so16179138_template_redirect', 0 );
function so16179138_template_redirect()
{
if( is_singular() )
{
global $post, $page;
$num_pages = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
if( $page > $num_pages ){
include( get_template_directory() . '/404.php' );
exit;
}
}
}

Resources