1

A code snippet from the Tuts Plus →

function link($atts, $content = null) {
    extract(shortcode_atts(array(
        "to" => 'http://net.tutsplus.com'
    ), $atts));
    return '<a href="'.$to.'">'.$content.'</a>';
}

Source Link →

Question →

How to reproduce "$to" in the shortcode?

I tried this →

[link $to= "https://wordpress.stackexchange.com/"] But this didnt worked.

2

It's just [link to= "https://wordpress.stackexchange.com/"].

This is a textbook example of why extract() is bad. You can't easily tell where variables are coming from. extract() creates variables out of an array with the keys becoming the variable name. So this part:

extract(shortcode_atts(array(
    "to" => 'http://net.tutsplus.com'
), $atts));

Is creating an array with a to key, set to http://net.tutsplus.com if it's not defined in $atts. Then it's extract()ed so that the to key becomes $to.

You should avoid using extract() and just use the $atts variable:

function link($atts, $content = null) {
    $atts = shortcode_atts(array(
        "to" => 'http://net.tutsplus.com'
    ), $atts);
    return '<a href="'.$atts['to'].'">'.$content.'</a>';
}
1
  • 1
    You should avoid using extract() and just use the $atts variable: → this is a Million dollar advise. thank you so much, sir. – The WP Intermediate Oct 14 '17 at 11:05

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.