skip to Main Content

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?

2

Answers


  1. maybe check how WordPress implemented it?

    https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/shortcodes.php

    and if the above link doesn’t work, check up WordPress core , how they implemented the short code functionality on GitHub.

    Login or Signup to reply.
  2. 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
         );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search