0

I have a Custom Post Type with a template named single-posttype.php. I'm using the Advanced Custom Fields plugin to display certain information (price and links to the actual product).

All that is working good. However, I want to take the links that I input in the Custom Fields and modify them to remove any query strings and append my affiliate code. I have a function that uses parse_url() and works fine for my purposes. The problem is it's not showing the string when I try to display it in other parts of the page.

Here's the full code of my template: http://pastebin.com/SrWrQ15L

Here's the function in question and how I call it:

        $theme_link_raw = get_field('theme_link');
        $preview_link_raw = get_field('theme_preview_link');
        $author_link_raw = get_field('author_link');

        $theme_parts = parse_url("$theme_link_raw");
        $preview_parts = parse_url("$preview_link_raw");
        $author_parts = parse_url("$author_link_raw");

        $theme_link = $theme_parts['scheme'] . "://" . $theme_parts['host'] . $theme_parts['path'];
        $preview_link = $preview_parts['scheme'] . "://" . $preview_parts['host'] . $preview_parts['path'];
        $author_link = $author_parts['scheme'] . "://" . $author_parts['host'] . $author_parts['path'];

        function affiliatize($link, $parts) {

          switch ($parts['host']) {
            case "themeforest.net":
              echo $link . "?ref=gorrillamcd";
              break;
            case "www.elegantthemes.com":
              echo "http://www.elegantthemes.com/affiliates/idevaffiliate.php?id=22953" . "&" . $link;
              break;
            case "themefuse.com":
              echo $link . "?r=42892";
              break;
            default:
              echo $link;
              break;
          }
        }


        $theme_affiliate = affiliatize($theme_link_raw, $theme_parts);
        $theme_affiliate_preview = affiliatize($preview_link_raw, $preview_parts);
        $theme_author = affiliatize($author_link_raw, $author_parts);

I know it's probably not perfect, but I'm not very familiar with PHP (I'm a ruby guy myself), so I'm thinking there is a simple problem I'm not seeing. Notice also this is a temporary hack. I plan to make it into a plugin, but that will take more time and learning and I'd like to go ahead and get it working now.

0

Ok, I'm stupid. I figured it was something simple.

In my function, I should have used return instead of echo. I was under the impression that echo was what's normally used since every example or tutorial I found on the internet for writing php functions used echo. At least now it makes more sense.

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.