WordPress.org

Ready to get started?Download WordPress

Forums

WP_Get_Referer & Divs??? (10 posts)

  1. ianbee
    Member
    Posted 1 year ago #

    I found out how to track where a click is from and then display elements based on where that click was from. I did it through "wp_get_referer". Here is the code I set up:

    <?
    $referer = $_SERVER['HTTP_REFERER'];
    
    if ( $referer == "http://www.testdomain.com/testpage" ) {
    	echo '<div id="bgphotos"></div>';
    } else {
        	echo '<div id="bgvideos"></div>';
    }
    ?>

    So this is telling my website to display the "bgphotos" div when a link is clicked on from "http://www.testdomain.com/testpage" and then display the "bgvideos" div when a link is clicked on from anywhere else. This works great!!

    Now the current problem I'm having is that on my main website I'm going to have a section called news, a section called videos and a section called photos. I'd like to track where these clicks are from just like I did above. But these sections are not pages, they are divs. So that is where it gets tricky.

    Again, I would like to track where clicks are from in a specific div / section and not just a page. If anyone knows if this is possible or knows a solution of any sort, please let me know. :) It would honestly mean soooo sooo, sooo, soo much to me, it honestly would!

  2. wpismypuppet
    Member
    Posted 1 year ago #

    JavaScript is your friend... You could simply use JavaScript to update the URL query string on click. Then you could use $_SERVER['QUERY_STRING'] and add that to your already existing if/else statement. Let me know if you need some help with that.

    jQuery will make short work of it since WordPress already has jQuery up and running.

  3. ianbee
    Member
    Posted 1 year ago #

    @wpismypuppet Hey man, I never even thought of using JS! It sounds quite simple how you explained it, but I honestly have no idea where to start. loll. I'm kind of a noob when it comes to JS, and even PHP. But yeah, I would like to track where clicks are from in a specific div / section and not just a page, so how exactly would I do that using Java or the query string? You said I could use JS to update the URL query string on click. I don't really know how to go about doing that. :( So so if I had a news section with blogposts under my home page would I have to specify the Query_string for the news section and then add that into my php if/else statement? I don't know how to do that or if that even made sense. But yeah it would be awesome if you could explain how to do it a bit more and as of right now I'm googling query strings and how they work :) Thanks for the help and if you want I could give you my e-mail to communicate faster!

  4. wpismypuppet
    Member
    Posted 1 year ago #

    You know what? The more I thought about it, the less JavaScript and $_SERVER['QUERY_STRING'] you need. In other words, you need zero of both. It's late, forgive me.

    Here's what you do... on the main page that has your sections, append the query string to the hyperlink itself. For example, your news section has a hyperlink to http://www.testdomain.com/testpage, right? Simply add ?fromwhere=news to the end. So something like:

    <a href="http://www.testdomain.com/testpage?fromwhere=news">click me</a>

    You get the idea... anyways, on your page that does the detection, I'd add the following (note that it's all your code from above with mine inserted):

    <?php
    $referer = $_SERVER['HTTP_REFERER'];
    
    if ( $referer == "http://www.testdomain.com/testpage" ) {
    	if( isset( $_GET['fromwhere'] ) {
    		if( $_GET['fromwhere'] == 'news' ) {
    			// Do something
    		}else if( $_GET['fromwhere'] == 'someothersection' ) {
    			// Do something else
    		}
    	}else {
    		echo '<div id="bgphotos"></div>';
    	}
    } else {
        	echo '<div id="bgvideos"></div>';
    }
    ?>

    This isn't tested, but should work just fine. It will at least point you in the right direction. Also, please note that when you write PHP code, you should ALWAYS open code blocks with <?php and not just <?. <? can also declare XML and some other languages, so it's best to forget the short code ever existed! :)

  5. ianbee
    Member
    Posted 1 year ago #

    @wpismypuppet awesome man! thank you so much! But the main problem is that the news posts, video posts and photos posts aren't just hyperlinks that I manually made, they are generated titles and thumbnails spit out through a plugin I'm using. But there is nothing I can do about that, I'll just have to dig through the coding and find a way to append the query string to the hyperlinks that the plugin generates. :)

    Thanks for all the help, if you don't mind could I tell you my e-mail and I could get back to you once I test this out fully?

    But again, thanks man.

  6. wpismypuppet
    Member
    Posted 1 year ago #

    Well, I'd have to see the code to help you better. Do you have a link to your website? I could check out the source and see more what you're talking about. We may have to rethink the JavaScript approach.

    The problem with changing the code in the plugin is that you'll lose those changes if and when you update the plugin! So that might not be the best option...

    And forgive me, but I don't give my email out here on the forum as I don't like to have random people email me with questions :) Simply respond to this thread and I'll get the message!

  7. ianbee
    Member
    Posted 1 year ago #

    @wpismypuppet Okay I tested the code and it broke all my single posts.
    Here is my web page I'm testing it on:

    http://www.enberwebsite.com/testtest/sample-page/

    There are four thumbnails at the top. Some goto external URL's. I got this to work with a few little tricks :). Anyways, The second thumbnail goes to an actual post, along with all the other smaller thumbnails below out. But like I said the single posts are broken at the moment. Here is what I did.

    1) Inserted this code into my single.php

    <?php
    $referer = $_SERVER['HTTP_REFERER'];
    if ( $referer == "http://www.enberwebsite.com/testtest/sample-page/" ) {
    	if( isset( $_GET['fromwhere'] ) {
    		if( $_GET['fromwhere'] == 'news' ) {
    			echo '<div id="news">news test</div>';
    		}else if( $_GET['fromwhere'] == 'videos' ) {
    			echo '<div id="videos">videos test</div>';
    		}
    	}else {
    		echo '<div id="bgphotos"></div>';
    	}
    }
    else {
        	echo '<div id="bgvideos"></div>';
    }
    ?>

    And I was going to put the "?fromwhere=news" code at the end of the php code that grabs the url for the thumbnails that link to posts but I didn't bother because the single posts aren't even working yet :(

    Do you know why this is??

  8. wpismypuppet
    Member
    Posted 1 year ago #

    Yup... I forgot a closing ) on the first if statement. Try this:

    <?php
    $referer = $_SERVER['HTTP_REFERER'];
    if ( $referer == "http://www.enberwebsite.com/testtest/sample-page/" ) {
    	if( isset( $_GET['fromwhere'] ) ) {
    		if( $_GET['fromwhere'] == 'news' ) {
    			echo '<div id="news">news test</div>';
    		}else if( $_GET['fromwhere'] == 'videos' ) {
    			echo '<div id="videos">videos test</div>';
    		}
    	}else {
    		echo '<div id="bgphotos"></div>';
    	}
    }
    else {
        	echo '<div id="bgvideos"></div>';
    }
    ?>
  9. ianbee
    Member
    Posted 1 year ago #

    Thanks so much for the help man. It works pretty much. I just need to test it with a few more things / plugins. I'm kinda worried that I'll lose contact with you. Are you sure I can't give you my e-mail? You don't have to post yours... If not its all good, I'll be posting with an update tommorow. :)

  10. Nathan Driver
    Member
    Posted 1 year ago #

    is there a way to just do it 'fromwhere' is a page - not categories?

Topic Closed

This topic has been closed to new replies.

About this Topic