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

Multisite homepage link WordPress

  • SOLVED

This question is a slight variation of an earlier problem raised at wpquestions.com/question/show/id/3081. The original question wasn't quite resolved unfortunately.

I have a WordPress Multisite install with the following characteristics:

1) Two types of sites, properties and cities
2) subfolder URL structure e.g. multisite.com/property1, multisite.com/property2, multisite.com/property3, multisite.com/city1, multisite.com/city2
3) property sites are all unique, but link to common city information when they share the same location e.g. if property1 & property2 are located in the same city, both will link to common information held on, say, multisite.com/city1
4) all sites (properties & locations) share the same theme

Across all sites (properties and cities) I require a custom homepage link. This homepage link should be created via a theme function or simple multisite-compatible plugin.

When a visitor browses from a property site to a city site, the homepage link on the city site should always link back to the referring property site homepage. Information about the referring property site should be stored (via cookie or session variable perhaps??), so no matter how many posts/pages/categories are browsed, the homepage link on the city site always references the homepage of the referring property site.

If a visitor is browsing a property site, say multisite.com/property1, the homepage link of property1 should be multisite.com/property1.

If a visitor browses to a city site directly from outside the multisite network (e.g. via a Google referral), the city site homepage link should link to the city site homepage.

It is highly unlikely a visitor would browse directly between similar sites e.g. from property1 to property2 or from city1 to city2 and thus this scenario can be ignored.

Hopefully the above problem makes some sort of sense. I’m guessing the solution maybe involves some combination of cookies, session variables and wp_get_referer().

Have a quick look at Abdessamad Idrissi’s answer to my initial question at wpquestions.com/question/show/id/3081 as I think this was getting pretty close to what I require.

Thank you.

Answers (2)

2012-01-17

John Cotton answers:

You marked the previous one as solved, but then don't say here why it turned out not to be....

Can you tell us?


designbuildtest comments:

Hi John, to be honest I can't remember exactly why it didn't work, unfortunately it just didn't work as hoped. I considered not including the link to the previous question as a) it might confuse people and b) the updated question is worded differently.

I'm not sure if the old question/thread is useful, or if people would prefer to discard the historical stuff and look at the question afresh.

Thanks.


John Cotton comments:

So why can't you just use the first site_url you come across in a session and stick with that?

ie

function get_custom_home_link() {
if( !isset($_SESSION['homelink']) {
$_SESSION['homelink'] = site_url();
}

return $_SESSION['homelink'];
}


John Cotton comments:

Sorry, that's missing a bracket:

function get_custom_home_link() {
if( !isset($_SESSION['homelink']) ) {
$_SESSION['homelink'] = site_url();
}

return $_SESSION['homelink'];
}


designbuildtest comments:

Hi John, just tested your code.

I began on property1 and the homelink URL was property1 - great.

Next, I clicked a hyperlink on property1 to a page on city1 and the homelink URL changed to city1 rather than pointing back to the property1 homepage.


John Cotton comments:

Are all your sites really on the same domain or have you just written it that way?

If they aren't then my code won't work as it relies on the session variable which, obviously, gets changed when you change domains!

What I don't know (but will try to find out) is whether WordPress does a session reset if you are using the sub-directory approach as you seem to be...


designbuildtest comments:

Thanks for the speedy responses John.

Yes, all sites are on the same domain.

I'm testing on my local server so URL's for homepages are localhost/property1, localhost/city1 etc.


John Cotton comments:

Just out of interest, why are you using multi-site?

Wouldn't the SEO be better as domain.com/city1/property1?


designbuildtest comments:

It's an Internal site, so no need for SEO. Property details are the focus, rather than the generic city information.


John Cotton comments:

Hi

$_SESSION definitely works across path-based multi-site installs (at least in version 3.3 on which I've just checked).

Have you actually started the session? You need:

session_start();

somewhere before the home link code is run (I tend to stick mine at the top of functions.php.


John Cotton comments:

PS Just tried my code on a path-based multsite and it works for me...:)


designbuildtest comments:

Hi John, adding
session_start();
before the function worked perfectly.

Your very simple solution was exactly what I required.

Thank you.


designbuildtest comments:

Hi John, as noted in Christianto's thread below, I'd like to split the prize value with both yourself and Christianto as both answers worked as I had hoped. I'll vote for the $35 prize money to be split $20 to yourself and $15 to Christianto.

Thanks again

2012-01-17

Christianto answers:

What if visitor browse from city site to property site?
do you want to show city site homepage link or property site homepage link?

You need to change homepage address to referring site if visitor browse other site on your network, but not change it if it browse from outside network?


designbuildtest comments:

Hi Christianto. The chances of a visitor starting their session on a city site and then browsing to a property site are tiny - this scenario can be ignored.

Yes, correct, to your second question. The homepage link doesn't change if the visitor begins their session via an external link from outside the multisite network.

Thanks


Christianto comments:

Hello,

have you find your solution?
if you haven't, please try my code:

function write_homepage_referer(){

$write_cookie_on_directory = array( '/property1', '/property2', '/property3' );

foreach($write_cookie_on_directory as $check_site){
// if match set cookie for 1 hour
if(strpos(get_bloginfo('url'), $check_site))
setcookie("sister_site_referer", get_bloginfo('url'), time() + 3600, COOKIEPATH, COOKIE_DOMAIN, false, false);
}
}
add_action('init', 'write_homepage_referer');


function get_custom_home_link($subdirectories){

$home_url = '';
$referer = strtolower( $_COOKIE['sister_site_referer'] );

// If no refferer return curent blog home
if ($referer == null || $referer == '' || $referer == get_bloginfo('url') ){

return get_bloginfo('url');

} else {

$ref = parse_url($referer);
$this_url = parse_url( get_bloginfo('url') );

// if domain same, from outside network
if( $ref['host'] == $this_url['host'] ) {

foreach($subdirectories as $subdirectory){
$checkreferer = strpos($_COOKIE['sister_site_referer'], $subdirectory);

if ( isset($_COOKIE['sister_site_referer']) && $checkreferer ){
$home_url = $_COOKIE['sister_site_referer'];
}
}

if($home_url)
return $home_url;
else
return get_bloginfo('url');

} else {

return get_bloginfo('url');

}
}
}


as you can see there are array that you can supply with your directory path on cookie creation function
$write_cookie_on_directory = array( '/property1', '/property2', '/property3' );

and called it by passing same array of your site directory path
echo get_custom_home_link( array( '/property1', '/property2', '/property3' ) );


Hope this help


designbuildtest comments:

Hi Christianto, your solution works perfectly - thank you.

I'd like to split the prize value with both yourself and John Cotton as both answers worked as I had hoped. I'd like to split the $35 prize money by voting $20 to John and $15 to yourself.

Sorry to not get back to you both sooner - I had to get some sleep last night.