Link to home
Start Free TrialLog in
Avatar of burnedfaceless
burnedfaceless

asked on

Creating a Shortcode with attributes

I want to create a shortcode with attributes for url and (link) text.

So the code

<a class="btn btn-lg btn-primary" role="button" href="http://example.com">Link Text</a>
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Do you mean parameters?
[mylink url="http://example.com" text="Link Text"]

Open in new window

Then in your functions.php or plugin
function mylink_shortcode( $atts, $content = null )
{
  $values = shortcode_atts( array(
    'url'  => '#',
    'text' => '(link)'
  ), $atts );
     
  return <<< URL
<a class="btn btn-lg btn-primary" role="button" href="{$values['url']}">{$values['text']}</a> 
URL;
}

add_shortcode( 'mylink', 'mylink_shortcode' );

Open in new window


You can read more about this here
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.