1

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?

3
  • 1
    You have to add the short code in the functions.php file of your theme. Does this work for you? Commented May 16, 2016 at 21:49
  • What do I need to add to the function file exactly? I'm trying to get this to display in a widget. Commented May 16, 2016 at 21:51
  • How did you go with this @suge1w? Commented Jun 14, 2016 at 22:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.