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

Custom home link WordPress

  • SOLVED

Hi. Please can someone help with the following problem.

I have a WordPress Multisite install. There are 10 sites on my multisite install, all using the same theme. Sites 1 to 9 each have their own unique content but all link to some common content held on site 10.

Within my theme I have a ‘Home’ link that leads a visitor back to each sites respective homepage. When a visitor clicks through from sites 1-9 to content held on site 10, I would like the site 10 “Home’ link to point back to the referring sites homepage (e.g. mysite.com/site3 – I am using a sub directory/folder install)

If the visitor continues to explore further categories/posts on site 10, the site 10 ‘Home’ link should always point back to the original referring sites homepage (e.g. the site 3 homepage).

I’m guessing the solution to this problem probably involves global variables and WordPress’s wp_get_referer() function.

Please can someone suggest a solution that I can pop into my functions.php file and call/reference throughout my theme (principally, in my header file).

Many thanks

Answers (4)

2011-10-02

Abdessamad Idrissi answers:

This is not easy as it seems :) but i came up with this:

function get_custom_home_link()
{
// check if referer is from another website on the Multisite
$referer = strtolower( wp_get_referer() );

$blog_list = get_blog_list();
$allowed_domains = array();

foreach ($blog_list as $blog) {
$allowed_domains[] = $blog['domain'];
}

// If no refferer return curent blog home
if ($referer == "")
return home_url( '/' );
else
{
// if cookies is saved
if ( isset($_COOKIE['site_10_referer']) )
return $_COOKIE['site_10_referer'];
else
{
$domain = parse_url($referer);
if (in_array($domain['host'], $allowed_domains))
{
// remember the domain for 10 minuts
setcookie("site_10_referer", $referer, time() + 600, COOKIEPATH, COOKIE_DOMAIN, false, false);
if ( SITECOOKIEPATH != COOKIEPATH )
setcookie("site_10_referer", $referer, time() + 600, SITECOOKIEPATH, COOKIE_DOMAIN, false, false);
} else
return home_url( '/' );
}

}
}

paste the above in function.php and call it where you generate the home link:
<a href="<?php echo get_custom_home_link(); ?>">Home</a>


Abdessamad Idrissi comments:

it will need a fine tuning but try it since I don't know if you have a multisite using plugin or the old multisite standalone installation.


designbuildtest comments:

Thank you Abdessamad. I think this is pretty close to what I require.

I am using the latest version of Multisite that was merged/incorporated into WP3.0, not the old MU version.

Please could you plug the following domains into your function so I can do a straight cut and paste...

Multisite install = http://localhost
Site 3 (as an example of sites 1-9) = http://localhost/auckland
Site 10 (shared content) = http://localhost/newzealand

Thank you.


Abdessamad Idrissi comments:

all right: try this on a live website, because in localhost the domain is always localhost and your muli-websites in localhost are considered sub directories not domain names!


function get_custom_home_link()
{
// check if referer is from another website on the Multisite
$referer = strtolower( wp_get_referer() );

$blog_list = get_blog_list();
$allowed_domains = array();

foreach ($blog_list as $blog) {
$allowed_domains[] = $blog['domain'];
}

// If no refferer return curent blog home
if ($referer == "")
return home_url();
else
{
$ref = parse_url($referer);
$home_url = parse_url( home_url() );

// if referer is from one of the multisites
// but not current site
if (in_array($ref['host'], $allowed_domains))
{
if( $ref['host'] == $home_url['host'] ) // if this is called by same website
{
if ( isset($_COOKIE['site_10_referer']) )
return $_COOKIE['site_10_referer'];
else
return home_url( '/' );

} else
{
// remember the domain for 10 minuts
setcookie("site_10_referer", $referer, time() + 600, COOKIEPATH, COOKIE_DOMAIN, false, false);
if ( SITECOOKIEPATH != COOKIEPATH )
setcookie("site_10_referer", $referer, time() + 600, SITECOOKIEPATH, COOKIE_DOMAIN, false, false);

return $_COOKIE['site_10_referer'];
}
} else
return home_url( '/' );
}
}


designbuildtest comments:

Hi Abdessamad, my live install will also be using be using a sub-directory URL format i.e. 'http://localhost/site3' will simply become 'http://www.mysite.com/site3' and so on.

Will your proposed solution work with a sub-directory install?

Thanks

2011-10-01

Romel Apuya answers:

Hi,

Sent you PM.

2011-10-01

Jonathan van Clute answers:

Get the referrer of the originating site, parse it, then add the path of the home page. Something roughly like:

$referer = str_replace(array('http://','https://'),'',$_SERVER['HTTP_REFERER']);
$referer_parts = explode('/',$referer);
$refer_domain = $referer[0];
$final_referer = $refer_domain.'/homepage/';

That's roughly one way to get there.

2011-10-02

John Cotton answers:

This isn't a direct answer to your question, but an alternative approach for your problem.

Instead of redirecting the the parent blog, why not just pull the content to the child content?

Create a blank page in the child blog with the same name as the page you want to pull from the parent blog and then set it to a template with this code in it:


<?php
/*
* Template Name: Page from Parent Blog
*/

get_header();

global $wp;

switch_to_blog(1);

query_posts( array( 'pagename' => $wp->request ));

restore_current_blog();

the_post();

?>

<div id="main">
.... <!-- whatever your typical html loop is -->
</div>

<?php
get_footer();
?>



Saves all the trouble!


designbuildtest comments:

Thanks John, good suggestion. I had actually contemplated this scenario but was keen to avoid the switch_to_blog() approach because I have read this can be computational expensive if used extensively thoughout a Multisite install. Cheers.


John Cotton comments:

<blockquote>because I have read this can be computational expensive if used extensively thoughout a Multisite install.</blockquote>

It's true, there is a fair amount of work done when switching blogs.

But if that is something that worries you (eg high traffic website), you could always cache the result. Instead of echoing the output, stick it into the buffer, store that and then send that result. Even if you held it for 30-60 minutes you could make a big difference.