Adding WordPress function into shortcode

I have a shortcode [table id=table /]

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

Read More

I want the slug to appear as the table id.

What am I doing wrong?

Related posts

1 comment

  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.

Comments are closed.