I want to construct a very basic shortcode solution in a simple non-WordPress website I am maintaining, to allow users to enter content that will be pulled from a database and rendered to screen, allowing them to create a href buttons.
I've seen various classes for this but want to do this in just a line or two of code if possible.
Only they will enter via a shortcode with this format:
[BUTTON link="www.test.com" label="Click here"]
So far I have this which doesn't extract the attributes properly:
$copy=preg_replace('/\[(BUTTON )(.*?)\]/', '<a href="$2">$1</a>', $copy);
Can anyone advise?
CodePudding user response:
maybe check how WordPress implemented it?
https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/shortcodes.php
CodePudding user response:
I assume that you don't actually want to capture the BUTTON, but the link and the label.
Code: (Demo)
echo preg_replace(
'/\[button link="([^"] )" label="([^"] )"]/i',
'<a href="$1">$2</a>',
$text
);