• Resolved Jarod Thornton

    (@jarmerson)


    Hi,

    I’m trying to implement conditions for the following.

    • if referer is external URL return false
    • if referer is home / front page return false
    • if referer is page from website, return the post ID URL in anchor and display title

    I’ve got everything working beautifully except if the referer is the home page etc.

    The main issue is with a home page slider and if you click on a slide the condition thinks it’s true and fires off the code and I don’t want it to do that.

    Here’s my code:

    $refering_page = get_the_title( url_to_postid(wp_get_referer()) );
    $home_refer = !is_home();
    $referer = wp_get_referer();
    if ($referer == $home_refer){
    if(!empty($_SERVER['HTTP_REFERER']))
    {
        if(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['HTTP_HOST'])
        {
            echo '<a href="Javascript:window.history.back();">&laquo; Back to '.$refering_page.'</a>';
        }
    }
    }
    else {echo 'nope';}

    Any insight is appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Jarod Thornton

    (@jarmerson)

    Ok I figured it out!

    Here’s the modified code that checks to see if the referring site is external source, home page, page, category, archive and if it is one of the two does nothing but if it is not it shows a window.history.back link with the referring page title

    $refering_page_title = get_the_title( url_to_postid(wp_get_referer()) );
    if (is_page() || is_category() || is_archive() || url_to_postid(wp_get_referer()) == 0)
    {
    	}
    elseif (!empty($_SERVER['HTTP_REFERER']))
    {
        if(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['HTTP_HOST'])
        {
            echo '<a href="window.history.back();">? Back to '.$refering_page_title.'</a>';
        }
    }
    Thread Starter Jarod Thornton

    (@jarmerson)

    I modified the code a little and cleaned it up.

    // Set exlusion for home page referrer, page pages, category pages, archive pages
    $exlusion =
    (get_the_ID() == 0) == (url_to_postid(wp_get_referer()))
    || is_page() || is_category() || is_archive() || is_front_page();
    // Grab the title of referring page
    if (!$exlusion) {
    	if (!empty($_SERVER['HTTP_REFERER'])) {
    		if (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['HTTP_HOST']) {
    			$referring_page_title =
    			get_the_title( url_to_postid(wp_get_referer()) );
    			// Set the conditions and create back link
    			echo '<a href="Javascript:window.history.back();">? Back to '.$referring_page_title.'</a>';
    		}
    	}
    }
    // Yay it works!

    I hope someone finds this useful.

    Thread Starter Jarod Thornton

    (@jarmerson)

    ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditions for refering entity’ is closed to new replies.