I have two wordpress website. One installed in main domain and another one installed in sub-domain under main domain. I'm doing this to manage 2 different language. I know there are so many plugins to manage multi language in site. But I have to do this manually.
But now i need to done a small job. I want to show a notification when any visitor will visit my main site for the 1st time. There I'll give 2 Language option to choose.
Option-1 Language English for main domain. (example.com)
Option-2 Language Spanish for Sub domain. (sp.example.com)
When visitor will select Option-1
he will stay in main site(domain)
. If he select Option-2
then he will be redirected to the another site(sub-domain)
and after this it'll happen automatically every time he visits
. But visitor can manually come back to main domain from sub-domain.
Any solution will be highly appreciated. Thanks
We can do this job by Cookie. I am assuming that your default landing domain is English one.
In functions.php of English domain write following code
add_action('init','is_new_visitor');
$is_new_visitor = false;
function is_new_visitor(){
check_new_visitor();
set_new_visitor();
}
function check_new_visitor(){
global $is_new_visitor;
//check if cookie set
if(isset($_COOKIE['new_visitor_lang']) &&
in_array($_COOKIE['new_visitor_lang'],array('en','sp'))){
$lang = $_COOKIE['new_visitor_lang']; // get cookie value
//if language is spanish then redirect user to spanish site
if($lang=="sp"){
wp_redirect('spanish lang domain');
exit;
}
}else{
//set global variable to true if it is new visitor
$is_new_visitor = true;
}
}
function set_new_visitor(){
// check the get param and redirect user to selected domain
if(isset($_GET['lang']) && in_array($_GET['lang'],array('en','sp'))){
setcookie( "new_visitor_lang", $_GET['lang'], time() + (365 * 24 * 60 * 60) );
if($lang=="sp"){
wp_redirect('spanish lang domain');
exit;
}
}
}
Now where you have to show the links, add following code (Some where in header.php)
global $is_new_visitor;
if($is_new_visitor){
echo '<a href="?lang=en">English</a>';
echo '<a href="?lang=sp">Spanish</a>';
}
Assuming you already have a pop up asking which version they want to see, and two buttons, or links, to take them to the appropriate site... you can use jQuery or plain JavaScript:
jQuery( document ).ready( function() {
var $days = 30;
var $date = new Date();
$date.setTime($date.getTime() + ($days * 24 * 60 * 60 * 1000));
var $expires = "; expires=" + $date.toGMTString() + '; path=/';
jQuery( '#id_of_english_button' )
.click( function( event ) {
document.cookie = 'language=english' + $expires;
});
jQuery( '#id_of_spanish_button' )
.click( function( event ) {
document.cookie = 'language=spanish' + $expires;
});
});
Then you'll have a PHP cookie variable you can work with: $_COOKIE['language']
Use what DarkBee had said in his comment, and do a header('location : sub.domain.com');
to send people to the sub domain if their cookie equals 'spanish'.
/*this code write on your main site and the user will
automatically redirect to the subdomain */
if(!isset($_SESSION['lang'])
{
$_SESSION['lang']='visitor selected language';
}
if($_SESSION['lang']=="Option-2")
{
header("Location:url");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.