I’m in the process of writing a parser for shortcodes. I have a regular expression that I’ve been playing with to retrieve ALL shortcodes within given text.
For example I have shortcodes like this
[url href="http://somedomain.com" class="123"] [snip category="spoons"]
my regex works fine for the above shortcodes but fails for this type
[widget id="23"]Body Text[/widget]
I’m looking to return an array like this…
[url href="http://somedomain.com" class="123"] [snip category="spoons"] [widget id="23"]Body Text[/widget]
then I’ll loop through each shortcode and parse them individually with a separate function. Below is the regex that i’ve been using.
"/[(.*?)(.+?)]/" <--works fine for no closing tags
and i tried this for the closing tags but it returns weird results
"/[(.*?)(.+?)](?:(.+?)?[/(.*?)])?/" <--- Doesn't work well/at all
any help is appreciated. Thanks in advance
WordPress has a built-in function (
get_shortcode_regex()) that returns the official regex that is used to parse shortcodes. My feeling is that you’d want to use that instead of rolling your own.
You can use the regex it returns to do with what you like.