[shortcode] Parameterized shortcode callback via new add_shortcode() arg (2 posts)

  1. spinality
    Posted 6 years ago #

    A new third argument to add_shortcode() would allow parameterized shortcodes via callback overloading:

    function add_shortcode($tag, $func, $merge_atts)


    add_shortcode('foo1', 'mycallback', array('arg1' => '1'))
    add_shortcode('foo2', 'mycallback', array('arg1' => '2'))

    I do lots of text replacements via shortcodes, and currently have to finagle this through content ([foo]arg1[/foo][foo]arg2[/foo] versus [foo1/][foo2/]).

    I can't think of any downside to parameterized callbacks.

  2. spinality
    Posted 6 years ago #

    Sorry to reply to my own post, but I now realize that the shortcode callback can determine which code matched the shortcode regex (via the hitherto undocumented third argument to the callback). This makes it possible to implement the above behavior by testing for shortcode name, rather than through default parameters on the add_shortcode() call. This could still be a useful feature but there is a good alternative already in place.

Topic Closed

This topic has been closed to new replies.

About this Topic