1

I have made some shortcode plugins according to my need. But this time I am getting a strange problem. My attributes in the shortcode are not getting the values written in the shortcode while executing them, instead I am just getting the default set values only. Please Help me.

add_shortcode('wi_form' , 'wi_form_func');

function wi_form_func($input) {
    extract(shortcode_atts( array(
        'productName'  => 'Java',
        'productPrice' => 1000
    ), $input));
    return $productName . esc_attr($productPrice);
}

1 Answer 1

3

Try not to use capital letters in the shortcode attributes, use for example

[wi_form product_name="php" product_price="888" ]

where:

add_shortcode( 'wi_form' , 'wi_form_func' );

function wi_form_func($input) {
    extract(shortcode_atts( array(
        'product_name'  => 'Java',
        'product_price' => 1000
    ), $input));
    return $product_name . esc_attr( $product_price );
}

or this:

add_shortcode( 'wi_form' , 'wi_form_func' );

function wi_form_func($input) {
    $input = shortcode_atts( array(
        'product_name'  => 'Java',
        'product_price' => 1000
    ), $input );
    return $input['product_name'] . esc_attr( $input['product_price'] );
}

if you don't want to use extract:

Update:

The shortcode attributes go through strtolower() in the shortcode_parse_atts() function that retrieves them from the shortcode tag (see here).

4
  • @user2046703 : if you put var_dump($input); at the top of your callback you will see that your attribute names come back all lowercase-- productName becomes productname. There is an explicit note in the Codex about this behavior: "Attribute names are always converted to lowercase before they are passed into the handler function."
    – s_ha_dum
    Commented Jul 14, 2013 at 16:17
  • @s_ha_dum thanks for the link. I updated the answer with a link to the source where the parsed attributes are taken through the strtolower() function.
    – birgire
    Commented Jul 14, 2013 at 18:05
  • Thanks birgire, Got my mistake.. and completed the target! :)
    – Sahil
    Commented Jul 15, 2013 at 10:09
  • ok great, please mark the answer as accepted if this solved your problem thanks.
    – birgire
    Commented Jul 17, 2013 at 9:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.