0

Someone helped accomplish this code last week which works perfectly, it pulls keywords entered into a custom field ( advanced custom fields ) into the middle of a URL and then redirects them.

Code in custom single template:

<?php $url = get_site_url(); $id = get_the_ID(); echo "<a href='{$url}/goto/amazon/{$id}/'>More info Test dynamic Amazon</a>"; ?>

Code in functions.php

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";
        wp_redirect($link);
        exit;
    }
});

Rewrites also in functions.php :

add_action( 'init', function(){
    add_rewrite_tag('%gotoamazon%','([^&]+)');
    add_rewrite_rule('^goto/amazon/(.*)/?','index.php?gotoamazon=$matches[1]','top');
});

But what id like to do now is use the same code to pull the wp_title if the keyword field is empty, I have the conditional setup, Ive then copied the code from above changing it to the title ( or what i thought would work ) but its not inserting the title into the desired place.

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

You're passing too many arguments to urlencode.

WordPress provides a couple of useful functions for these situations. add_query_arg is a helper to build a URL with query arguments, and esc_url provide sanitation and validations relevant to encoding URLs.

Also, wp_title will display or return a combination of the page/post title and a seperator, which doesn't seem to be what you need. I'm assuming you want the page/post title, so I've used the get_the_title function and passed it the Global $post variable.

add_action( 'template_redirect', function(){

  if ( ! is_singular( 'post_type_key' ) ) {
    return;
  }

  Global $post;
  $default = get_the_title($post);
  $keytitle = get_query_var( 'gotoamazon', $default );
  $args = array(
    '_encoding' => 'UTF8',
    'camp' => '1634',
    'creative' => '19450',
    'field-keywords' => $keytitle,
    'linkCode' => 'ur2',
    'tag' => 'AFFID'
  );
  $link = add_query_arg($args, 'http://www.amazon.co.uk/s/' );
  wp_redirect( esc_url( $link ) );
  exit;
});
4
  • Sorry Andrew, I should have said the URL provided is an affiliate link so it needs to retain its structure. So the wp_title will basically replace the search keywords where "{$keytitle}" is in the URL, which has the AFFID at the end. ( apprently amazon are funny about any part of their URL's being removed ) I did try your code to see if it worked, but im getting the same result where no keywords(wp title) are being passed to amazon as a search – Randomer11 Jan 9 '18 at 15:21
  • Have amended the code to retain the structure of the link. Can you tell me the file you're running your code from? Is it in functions.php? – Andrew Jan 9 '18 at 16:31
  • Hi Andrew, yes the code is running from functions.php. The first bit of code i posted above is then running from a custom single.php . So when the link is clicked, it should goto the amazon URL , inserting the wp title into the middle of the URL ( which makes amazon search for a certain type of product ) – Randomer11 Jan 9 '18 at 16:33
  • Tested the new code and it does the same unfortunately – Randomer11 Jan 9 '18 at 16:54

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.