I'm not sure if I don't fully understand the basics of functions in php or maybe it is WP convention that I am not sure of. My question is why use an argument in a function when it is not going to be used?

take the following code for example.

add_shortcode( 'tf', 'ch2ts_twitter_feed_shortcode' );

function ch2ts_twitter_feed_shortcode( $atts ) {$output = '<a href="http://twitter.com/ylefebvre">Twitter Feed</a>';
    return $output; }

$attr is not even used but still passed as an argument? Please explain.


In general, if arguments is not used or the defaults values are to used of an argument/parameter, you don't have to write it out, they can simply be omitted. The only time when you have to pass any value to an argument is when a function expect a valid value to be passed to it in order to properly execute, something like get_terms() which expects the first argument to have a valid taxonomy value

In your example, using shortcodes, it is not necessary to pass $atts as you are not expecting any values from attributes and you have not set up any attributes inside your shortcode function. Your shortcode simply returns a static value. In a case like this, you can simply just omit $atts


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.