0

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

1 Answer 1

1

$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' );
Sign up to request clarification or add additional context in comments.

8 Comments

Nice, but I was was more looking for two options like if parameter is 'se' the the return should be a string in swedish or if the parameter is 'en' the return should be a string in english
Then write a conditional...in place of the return above
if ( $a['language'] == 'en' ) { //do something } elseif ( $a['language'] == 'se' ) { //do something else } else { // return }
OK, like this then: if ( $a['language'] == 'en' ) { $language = "Hello"; } elseif ( $a['language'] == 'se' ) { $language = "Hej"; } else { // $language = "A default value"; } return $language;
Perhaps an option also if the is an empty parameter that will return a default value? Like elseif ( $a['language'] == '' ) { $language = "Tag" } Tag = hello in german
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.