0

I am trying to display content in a custom template based on referral URL. I put a link in my footer called http://domain.com/?ref=special and image2.gif shows still when I click it and go to the page. I need image1.gif to show if there is traffic coming from a domain that ends with ?ref=special. How can I do this and what am I missing?

   <?php
      $ref1 = parse_url(wp_get_referer()); //getting the referring URL
      if($ref1["path"]=='/?ref=special/')
  :?>
    <div id="featured_image_small" style="background: url('<?php echo esc_url( home_url( '/wp-content/uploads/2015/05/image1.gif' ) ); ?>') no-repeat scroll 0 0 transparent; background-size:cover;">
  <?php else: ?>
    <div id="featured_image_small" style="background: url('<?php echo esc_url( home_url( '/wp-content/uploads/2015/04/image2.jpg' ) ); ?>') no-repeat scroll 0 0 transparent; background-size:cover;">
  <?php endif ;?>
  • Try using if($_GET['ref'] == 'special'){// do this}else{//do that} – gdaniel May 19 '15 at 21:27
  • your if test seems wrong, why would there be a trailing slash? – Milo May 19 '15 at 21:33
  • Hi that does not work for me. I tried <?php if($_GET['ref'] == 'special'){echo "hello world";} else{} ?> – JediTricks007 May 19 '15 at 21:43
0

a $_GET variable would be the better option.

try var_dump($_GET); on the page before the conditional and see exactly what you need to test against

It seems that there is something more to the puzzle that we are missing. Is the file not showing up correctly? Could that be because the file paths are wrong? Or is it always showing the wrong image?

Also, I would say to save the image-filename as a variable based on that conditional to keep your code a little cleaner.

if ($_GET['ref'] == 'special') { 
  $file = '/wp-content/uploads/2015/05/image1.gif';
} else {
  $file = '/wp-content/uploads/2015/04/image2.gif';
}
?>

<div id="featured_image_small" style="background: url('<?php echo esc_url( home_url( $file ) ); ?>') no-repeat scroll 0 0 transparent; background-size:cover;">
| improve this answer | |
0

The following worked for me.

functions.php

function add_query_vars_filter( $vars ){
  $vars[] = "ref";
  return $vars;
}

custom template

  <?php
       if($_GET['ref'] == 'special')
   :?>
     //do something
   <?php else: ?>
    //do something
  <?php endif ;?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.