3

For some reasons every shortcode, that I try to add to functions.php, displays only default attribute value and ignores what I specify in a post. E.g.:

function my_shortcode( $atts ) {
    extract( shortcode_atts(
        array(
            'id' => '18',
        ), $atts )
    );
    return $id;
}
add_shortcode( 'sc', 'my_shortcode' );

So I use shortcode [sc id="81"] and expect echoing 81 but I get 18. BTW I use child theme, and latest version of wordpress. I'm really stuck. Thanks for any help.

2
  • Did you make sure it's not a cache issue? Try changing 18 to something else. Commented Sep 20, 2020 at 23:34
  • avoid using extract here as it may result in broken-ness and it can overwrite globals making this a security issue too Commented Sep 28, 2022 at 12:43

1 Answer 1

1

I just tried your exact code, putting the PHP code you pasted above in my theme's functions.php file, and inserting the shortcode as you typed [sc id="81"] in to a page in my site, publishing the page, and it correctly outputted a value of 81 on my published page.

So something in your theme or a plugin must be conflicting with it.

Try changing to one of the default WordPress themes and testing it again, and/or disabling your plugins.

I suspect it could be a conflict because the parameter is named 'id' - so you could also try using a different parameter name (eg. 'myid').

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.