I'm trying to construct an easy to use affiliate site, each post-type has a custom field where keywords can be entered per post. ( using advanced custom fields plugin )

Then in a template I call that field into a URL like this :

<?php if( get_field('amazon_keywords') ): ?>
            <a href="http://www.amazon.co.uk/s/?_encoding=UTF8&camp=1634&creative=19450&field-keywords=<?php the_field('amazon_keywords'); ?>&linkCode=ur2&tag=AFFID"> More info Test dynamic Amazon</a>
    <?php else : ?>
            Something else
    <?php endif; ?>

This works and displays the link with the entered keywords.


Because its an affiliate link, best practice would be to mask it. So instead of showing the affiliate link id like to show it something like :


Normally this is fine because I can just do a redirect, but the addition of the php inside the URL for the keywords is where I've stumbled.

Anyone have any ideas ?


Without touching rewrites - you could probably do somthing like

website.com/?goto-amazon=<?php echo get_the_ID() ?>

Which'ed output


Where 42 is an example of a post that contains the amazon_keywords.

In your site you could listen for the goto-amazon query and redirect thereafter

add_action( 'init', function(){
    if (isset($_GET['goto-amazon'])) {
        $keywds = get_field('amazon_keywords',intval($_GET['goto-amazon']));
        $link = "http://www.amazon.co.uk/s/?_encoding=UTF8&camp=1634&creative=19450&field-keywords={$keywds}&linkCode=ur2&tag=AFFID";


To do with rewrites you could so something like so. On your post

$url = get_site_url();
$id = get_the_ID();
echo "<a href='{$url}/goto/amazon/{$id}/'>go to Amazon</a>";

Which'd make a link like


Then in your functions

add_action( 'init', function(){

    // uncomment reload once or twice, or hit SAVE in wp-admin > Settings > Permalinks


add_action( 'template_redirect', function(){
    global $wp_query;
    if ($goto = get_query_var( 'gotoamazon' )) {
        $keywds = urlencode(get_field('amazon_keywords',intval($goto)));
        $link = "http://www.amazon.co.uk/s/?_encoding=UTF8&camp=1634&creative=19450&field-keywords={$keywds}&linkCode=ur2&tag=AFFID";

When dealing with rewrites, make sure you reset the rewrite rules, instructions in commented code above.

I don't know how amazon expects the keywords, but they should be urlencode()'ed

  • That looks like a great solution, ( although would be nicer with /goto/amazon/42 ) but seems to fail when clicking the link. Just gives a blank "Wordpress error" page.
    – Randomer11
    Jan 5 '18 at 20:24
  • sorry I did that off the top of my head. the error is from wp_die(); it should be exit. Jan 5 '18 at 21:35
  • Ah yes thats working now, unfortunately however the keywords are not passing to the URL when it goes through to amazon. Im using advanced custom fields plugin if that makes any difference ?
    – Randomer11
    Jan 5 '18 at 22:03
  • Maybe url_encode() them? Jan 5 '18 at 22:57
  • I've updated the post to include url rewrites, so you can get the /goto/amazon/##/ Jan 6 '18 at 0:20

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.