1
votes

I have a shortcode [table id=table /]

I want to do something like this <?php echo do_shortcode('[table id="'.$post->post_name.'"/]'); ?>

I want the slug to appear as the table id.

What am I doing wrong?

1
You have to add the short code in the functions.php file of your theme. Does this work for you? - wawa
What do I need to add to the function file exactly? I'm trying to get this to display in a widget. - suge1w
How did you go with this @suge1w? - Tim Malone

1 Answers

1
votes

Given that you mentioned in the comments that you're trying to use this shortcode in a widget - widgets don't parse shortcodes by default.

To make most widgets parse shortcodes (most being those with text fields that apply the widget_text filter), you can add the following to your theme's functions.php:

add_filter("widget_text", "do_shortcode");

You can then refer to this shortcode directly in your widget text like you would expect you can:

[table id=... /]

EDIT:

If you have any trouble running shortcodes that are on their own line, it may be because they get wrapped in <p></p> tags automatically by Wordpress. To stop this happening, add this filter before the one added above:

add_filter("widget_text", "shortcode_unautop");

The shortcode_unautop() function simply stops shortcodes from being wrapped in paragraph tags like is Wordpress' default behaviour in most text fields.