Regex, WordPress, and the Search Regex plugin - converting a script to a shortcode

69 Views Asked by At

I am using WordPress and the Search Regex plugin.

I need to covert several hundred Posts that contain a script which contains a unique Photo album number.

Each script needs to be changed to a shortcode which contains the same Photo album number.

Here is an example:

Search for the script:

<tt>%%wppa%%</tt> <tt>%%album=15%%</tt>

and replace it with the shortcode:

[wppa type="album" album="15"][/wppa]

What would I place in the Search field?

and

What would I place in the Replace field?

1

There are 1 best solutions below

3
Laurel On

Use this regex:

<tt>%%([^%]+)%%</tt>\s*<tt>%%([^%=]+)=([^%=]+)%%</tt>

and this replacement:

[$1 type="$2" $2="$3"][/$1]

The $ numbers in the replacement refer to the capture groups: (). Overall, it's very simple, with [^%]+ matching "not %", \s* matching whitespace, and [^%=]+ matching "not % or =".

The plugin uses PCRE, but I'm not sure if the regex needs any adjustments since I don't use the plugin. (It may require escaping the / characters.)