[wp-trac] [WordPress Trac] #54871: Specify which params are required in add_shortcode callback

WordPress Trac noreply at wordpress.org
Sat Jan 22 15:26:33 UTC 2022

#54871: Specify which params are required in add_shortcode callback
 Reporter:  mkarimzada   |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Shortcodes   |     Version:  5.8.3
 Severity:  trivial      |  Resolution:
 Keywords:  php8 close   |     Focuses:  docs
Changes (by jrf):

 * keywords:  needs-dev-note needs-codex needs-docs php8 => php8 close


 @mkarimzada Thanks for opening this ticket.

 I must be missing something, but as far as I can see the docblock
 specifies exactly which parameters are expected and in which order on the
 lines just below the one you linked:

 > Every shortcode callback is passed three parameters by default,
 > including an array of attributes (`$atts`), the shortcode content
 > or null if not set (`$content`), and finally the shortcode tag
 > itself (`$shortcode_tag`), in that order.


 When I read that, I come to a function signature of:
 function my_shortcode_callback($atts = array(), $content = null,
 $short_tag = '') {}

 // Or in typed form:
 function my_shortcode_callback(array $atts = array(), ?string $content =
 null, string $short_tag = '') {}

 The first two parameters are "optional" as the shortcode when used:
 * may not be passed any attributes `[my_shortcode]content[/my_shortcode]`
 * may not be passed any content `[my_shortcode att="value" /]`

 The third parameter is optional as, if the callback function is used only
 for that one shortcode, it won't be needed in the callback function

 WordPress will always pass all three parameters, but the callback function
 definition can leave off the third, the second and third or even all

 Note: PHP 8.0 named parameters are not supported, so these parameters are
 passed positionally.

 So what is the enhancement you expect here ?

Ticket URL: <https://core.trac.wordpress.org/ticket/54871#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list