3D-kreativ
3D-kreativ

Reputation: 9297

How to handle parameters in shortcode function

I'm doing my first shortcode for WordPress. The function that I'm doing will return different translation of a string depending on the parameter se for swedish or en for english. So I'm going to use a simple if statement or a switch statement, but which of the two option below are the best and what's the difference between them? Should I use $a or language to check the in parameter? The default value is se for swedish.

$a = shortcode_atts( array(
'language' => 'se',
), $atts );

or

extract(shortcode_atts(array("language"=>"se"),$atts));

Upvotes: 0

Views: 441

Answers (1)

rnevius
rnevius

Reputation: 27102

$a['language'] will give you the value of the language key.

Or a full example:

// [my_shortcode language="value"]
function my_shortcode_function( $atts ) {
    $a = shortcode_atts( array(
        'language' => 'se',
    ), $atts );

    if ( $a['language'] == 'en' ) {
        $language = 'Hello';
    } elseif ( $a['language'] == 'se' ) {
        $language = 'Hej';
    } else {
        $language = 'Incorrect language specified';
    }

    return $language;
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );

Upvotes: 1

Related Questions