Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect visitors to subdomain / logged in users to main domain WordPress

  • SOLVED

Hi everyone,

I want to redirect all visitors except logged in users to a sub-domain (temporary site), while logged in users are directed to the main domain (site under construction to be launched).

I tried inserting a condition in wp-blog-header.php


<?php
/**
* Loads the WordPress environment and template.
*
* @package WordPress
*/

if (is_user_logged_in()) {

if ( !isset($wp_did_header) ) {

$wp_did_header = true;

require_once( dirname(__FILE__) . '/wp-load.php' );

wp();

require_once( ABSPATH . WPINC . '/template-loader.php' );

}
}
else {

wp_redirect('http://evenement.tamtamsoie.net/', 302);
exit;

}

?>



but i get the following error : "call to undefined function".

Not being a PHP (or WP) pro i guessed that i could not call the is_user_logged_in function before WP is loaded ... But can't figure out where to insert my condition.

My question : i could i achieve conditional redirection on visitor status?

Thx

Answers (5)

2011-12-19

Julio Potier answers:

Hello

Do not hack the WP core !!

Paste this code into a plugin file into your plugins folder :
[[LINK href="http://pastebin.com/wsdvQ31q"]]http://pastebin.com/wsdvQ31q[[/LINK]]

Example <em>/wp-content/plugins/tempo-redirection/file.php</em>

See you !

ps : Francisco, Robin, why did you let her hacking the WP core !? Be expert please...


annabelR comments:

Thx Julio.

You're right, i guess that i shouldn't even try messing with WP core code : - )
Alas, the pastebin link you posted is broken.
But I'll try Ivaylo's plugin approach and will let you all know of the outcome.

Cheers,



Julio Potier comments:

Sorry, this is the correct (new) pastebinn plugin code i've made for you.

http://pastebin.com/aiQFzUwD

If you use a plugin system, please bear with me.


annabelR comments:

Thx Julio : yours and Ivaylo's suggestions were very similar... but i couldn't get the plugin approach right : - (
So i opted for inserting the snippet in my theme's functions.php file, instead.

It's doing the trick.

Again : many thx.
Cheers

2011-12-19

Ivaylo Draganov answers:

Hello,

you should make the detection and redirections once WP has been loaded. No core hacking is neither necessary, nor desired. So, it's best to make a plugin for that. It is as simple as using this code:


<?php
/**
* Plugin Name: My Redirection
* Description: Redirect not logged in users
*/
add_action( 'get_header', 'wpq_my_redirection' );

function wpq_my_redirection() {

if ( ! is_user_logged_in() ) {

wp_redirect( 'http://evenement.tamtamsoie.net/', 302 );

}

}


You can place it in a new file under <em>wp-content/plugins</em> or <em>wp-content/mu-plugins</em> or just paste it in your <em>functions.php</em>.


annabelR comments:

Hi Ivaylo,

Here's what i've done : i inserted the code you suggested in my theme's functions.php...
And... all is working fine : THX!

PS : the plugin method didn't work out (php file named myredirection.php placed at the root of WP plugins directory or in a sub-directory named my-redirection).


2011-12-19

Francisco Javier Carazo Gil answers:

Hi Annabel,

Where are you calling this function?

See this: "wp_redirect() is located in wp-includes/pluggable.php".


Francisco Javier Carazo Gil comments:

You can use a "require_once('wp-includes/pluggable.php')" for example.

Maybe you can use a plugin like this one: http://wordpress.org/extend/plugins/peters-login-redirect/

If need more help tell me.


annabelR comments:

Hi Xavier,

So far i'm calling it in the wp-blog-header.php.

I used the require_once and rewrote my condition using Robin suggestion : !is_user_logged_in

As follow


<?php
/**
* Loads the WordPress environment and template.
*
* @package WordPress
*/
require_once('wp-includes/pluggable.php');

if (!is_user_logged_in()) {

wp_redirect('http://evenement.tamtamsoie.net/', 302);
exit;

} else {

if ( !isset($wp_did_header) ) {

$wp_did_header = true;

require_once( dirname(__FILE__) . '/wp-load.php' );

wp();

require_once( ABSPATH . WPINC . '/template-loader.php' );


}
}

?>



Only this time i get the following error message :
<blockquote>Fatal error: Call to undefined function is_ssl() in /public_html/wp-includes/pluggable.php on line 604</blockquote>

Oh... the plugin you suggest won't do the trick (i thought so but gave it a try nonetheless), i'm looking for a solution to apply both prior to login (all other visitors) and after

Way out of my league... i am
: - (

Thx for your help.


Francisco Javier Carazo Gil comments:

Hi Isabel,

As Julio says, it's better to not hack se WP-Core. Sorry but I have answer so fast and I was thinking only in PHP and not in WP + PHP when I have seen the error.

I recommend you what Ivaylo or Gabriel says.

If you use Ivaylo option, remember:
1. Create the directory in /wp-content/plugins/ -> my-redirection
2. Create a file my-redirection.php
3. Paste the Ivaylo's code
4. Log in admin
5. Go to plugins
6. Activate it and that's all

If any doubt, ask.

2011-12-19

Arnav Joy answers:

please ignore my previous answer

you do not have to write any code to your theme files. and that will be good.

try using this plugin

http://wordpress.org/extend/plugins/peters-login-redirect/

2011-12-19

Gabriel Reguly answers:

Hi annabelR,

This is a different approach, but might be easier as you will not need to create a plugin or edit WordPress files.

Use this plugin [[LINK href="http://wordpress.org/extend/plugins/simplesplash/"]]http://wordpress.org/extend/plugins/simplesplash/[[/LINK]]

And within your splash.php (you need to create this file at your theme directory) you can test for logged in and do your redirection.


<?php
if ( is_user_logged_in() ) {
wp_redirect('http://tamtamsoie.net/', 302); // main site
} else {
wp_redirect('http://evenement.tamtamsoie.net/', 302); // subdomain
}
?>


Be aware that you could just show your message right at splash page, no need for a subdomain.

Regards,
Gabriel