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;
});