最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Where to filter Shortcode string prior to execution

programmeradmin2浏览0评论

Whatever WP function assembles the $att array from the [myShortcode att1="..." att2="..." ...] string uses the " character to delimit the data. However, the and the characters seem to be read as text, not delimeters; and their use in place of " can lead to shortcode errors.

In theory, there shouldn't ever be a problem because and are not characters on the qwerty keyboard, and so would not be encountered with manual entry. Yet, cutting and pasting from sources that use and allow for their inclusion (and there should be error checking for that situation). For example, the page The Beginner’s Guide to Writing Your Own Custom Shortcode says that [helloworld name=”Bob”] outputs Hello Bob!, which is not the case because the webpage cutely styled " into . Cutting and pasting [helloworld name=”Bob”] outputs Hello ”Bob”!; and [helloworld name=”Bob Smith”] outputs Hello ”Bob!, since the space between Bob and Smith is read as a delimeter and the array says that [name] = "Bob and that [0] = Smith.

Doing a preg_replace on the shortcode string before it is parsed into the $atts array seems like the best way to prevent errors. The question is where to do that.

Whatever WP function assembles the $att array from the [myShortcode att1="..." att2="..." ...] string uses the " character to delimit the data. However, the and the characters seem to be read as text, not delimeters; and their use in place of " can lead to shortcode errors.

In theory, there shouldn't ever be a problem because and are not characters on the qwerty keyboard, and so would not be encountered with manual entry. Yet, cutting and pasting from sources that use and allow for their inclusion (and there should be error checking for that situation). For example, the page The Beginner’s Guide to Writing Your Own Custom Shortcode says that [helloworld name=”Bob”] outputs Hello Bob!, which is not the case because the webpage cutely styled " into . Cutting and pasting [helloworld name=”Bob”] outputs Hello ”Bob”!; and [helloworld name=”Bob Smith”] outputs Hello ”Bob!, since the space between Bob and Smith is read as a delimeter and the array says that [name] = "Bob and that [0] = Smith.

Doing a preg_replace on the shortcode string before it is parsed into the $atts array seems like the best way to prevent errors. The question is where to do that.

Share Improve this question asked Feb 18, 2022 at 22:57 jcroppjcropp 1112 bronze badges 1
  • I'd refer to the official shortcode API reference vs a random blog that has formatting issues. - codex.wordpress.org/Shortcode_API – Bazdin Commented Feb 19, 2022 at 3:04
Add a comment  | 

1 Answer 1

Reset to default 0

shortcode_atts_{$shortcode} seems to be the WordPress filter you need. https://developer.wordpress.org/reference/hooks/shortcode_atts_shortcode/

It is used in the shortcode_atts() function: https://developer.wordpress.org/reference/functions/shortcode_atts/

发布评论

评论列表(0)

  1. 暂无评论