0

I am trying to create a shortcode that will return a link to a book on Amazon with a specific affiliate tracking code specific to the author. I created a user option called "amz" to hold the author's tracking code. This works fine when I'm using it elsewhere. This is the shortcode I created:

function amazon_link_function($atts, $content = null) { extract(shortcode_atts(array( 'isbn' => ' ', 'title' => ' ' ), $atts));

  $amztrack = the_author_meta( 'amz' );

  return '<a href="http://www.amazon.com/exec/obidos/ASIN/' .$isbn. '/' .$amztrack. '" title="' .$title. '" target="_blank">' .$title. '</a>';

} add_shortcode("amazon", "amazon_link_function");

The isbn and title values return properly, but the tracking code stored in the $amztrack variable is displayed as plain text before the paragraph the shortcode is used in:(

I also tired this for the return line and got the same result:

  return '<a href="http://www.amazon.com/exec/obidos/ASIN/' .$isbn. '/' .the_author_meta( 'amz' ). '" title="' .$title. '" target="_blank">' .$title. '</a>';
1

the_author_meta() prints the content immediately. Use instead:

$amztrack = get_the_author_meta( 'amz', FALSE );

This will return the value.

  • Boom! I havn't even finished editing the question for format and you solved it. Thanks! – JPollock Mar 1 '13 at 22:15
1

shortcodes need to return the value, therefore you cannot use a function which prints the output, like the_author_meta();

use get_the_author_meta() http://codex.wordpress.org/Function_Reference/get_the_author_meta

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.