WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional Tag for Search Engine Referrals (11 posts)

  1. ALTexasGirl
    Member
    Posted 7 years ago #

    Does anyone know how to code a conditional tag in php that will only show certain content, especially ads, to visitors that enter the site via a search engine or a predetermined domain.

    I get a lot of hits from sites like Yahoo News, Topix, etc and I'd like to display ads only to them and not my regular readers.

    Thanks!

  2. Austin Matzko
    Member
    Posted 7 years ago #

    $url = parse_url($_SERVER['HTTP_REFERER']);

    Then $url['host'] should return www .google.com or google.com from the referrer. Then you could run through an array of search sites:

    $sites = array('google','yahoo');
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    // etc.

  3. ALTexasGirl
    Member
    Posted 7 years ago #

    filosofo,

    Thanks so much for the reply. I'm still learning a lot of php codes, but where do I need to implement this code and where I should add the adsense or chitika code? Sorry to be such a bother . . .

  4. davidchait
    Member
    Posted 7 years ago #

    that code could easily go at the top of header.php.

    ads themselves (with the "if ($show_ad)" wrapper) can go anywhere you want. looks like you've already customized a ton. and I don't know what your theme layout is. index.php would normally be the main block, and between-post ads could go there (as could ads at the start or end of the block). sidebar.php is generally one (or more) sidebars, so if you want ads there that's where to look.

    -d

  5. ALTexasGirl
    Member
    Posted 7 years ago #

    Okay, I installed this code in my header:


    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo');
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    // etc.

    and this code in my comments template (the spot where I want the ads)


    <?php if ($showad)?>
    {{{{Adsense code}}}}
    <?php } ?>

    Now I'm getting a parse error message above the header and another parse error in the comments. You can see it here: http://www.onthebaseline.com/2006/05/12/darkness-halts-quarterfinal-play-at-the-german-open/

    Thanks for the help.

  6. Austin Matzko
    Member
    Posted 7 years ago #

    Okay, you have to close all the curley brackets { }. So my example fleshed out would look more like
    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo');
    $show_ad = false;
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    }
    }

    But what's causing the error I see on your page now is that PHP must surrounded by opening and closing tags, like so: <?php . . . ?> Otherwise the server doesn't know to parse it.

  7. ALTexasGirl
    Member
    Posted 7 years ago #

    I fixed the header, but for some reason it still will not work.

    Here is what I have now:

    HEADER -

    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo');
    $show_ad = false;
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    }
    }

    COMMENTS

    <?php if ($showad ?>
    ADSENSE CODE

    ?>

    Does it matter where I put the code in the header template? Thank you so much for all your help . . .

    I'm still seeing the same error message, but I temporarily removed the code as it was interfering with my comments template.

  8. Austin Matzko
    Member
    Posted 7 years ago #

    More like this:

    <?php if ($show_ad) { ?>
    ADSENSE CODE
    <?php } ?>

    (and if Adsense code is PHP, it needs to be within <?php . . . ?>)

  9. ALTexasGirl
    Member
    Posted 7 years ago #

    Okay - thanks - that took care of the problem with the code error by the adsense block, but I'm still getting the header error. This is where I've placed the code:

    <?php get_archives('monthly', '', 'link'); ?>

    <?php wp_head(); ?>

    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo');
    $show_ad = false;
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    }
    }

    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>

    Do I have it in the wrong spot?

  10. davidchait
    Member
    Posted 7 years ago #

    No, and not to seem negative here, but learning a teensy bit about PHP would get you a LONG way (and would have had your solution for you already... ;) ;) ).

    The php code block in the header should have a <?php before it and a ?> after it...


    <?php
    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo');
    $show_ad = false;
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $show_ad = true;
    }
    }
    ?>

    -d

  11. ALTexasGirl
    Member
    Posted 7 years ago #

    davidchait . . .

    Thanks for the help. I'm working at learning php, but some things still allude me.

Topic Closed

This topic has been closed to new replies.

About this Topic