Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress shortcode parameters

Is there a way to get all parameters in the WordPress shortcode?

For example,

function bartag_func( $atts ) {
    $a = shortcode_atts(
             array(
                 'foo' => 'something',
                 'bar' => 'something else',
             ),
             $atts);

    return "foo = {$a['foo']}";
}
add_shortcode('bartag', 'bartag_func');

Here [bartag] is shortcode.

Is there a way to know the values of shortcode_atts or value of foo bar, etc.?

Is there a way to get all variable values inside the bartag_func?


1 Answers

You can use this function to get all values from shortcode:

function bartag_func( $atts ) {
    $atts = shortcode_atts(
        array(
            'foo' => 'no foo',
            'bar' => 'default bar',
        ), $atts, 'bartag');

    return 'bartag: ' . $atts['foo'] . ' ' . $atts['bar'];
}
add_shortcode( 'bartag', 'bartag_func' );

[bartag foo="koala" bar="bears"] outputs the following: bartag: koala bears

[bartag foo="koala"] outputs the following: bartag: koala default bar

like image 186
Gopal S Rathore Avatar answered Sep 13 '25 18:09

Gopal S Rathore