styler
styler

Reputation: 16501

Adding an if clause inside php function to only display html attribute if parameter equals true

I'm creating a wordpress button shortcode and would like to pass in 3 parameters - class, type and then true/false depending on if the link should be external or not. At the moment however I'm not sure how to wrap this attribute inside the function and was wondering if anyone could advise me on how to solve this?

PHP

function button($atts, $content = null) {
   extract(shortcode_atts(array('link' => '#', 'type' => '', 'external' => 'false'), $atts));
   return '<a href="/'. $link .'" class="btn" ' . if( 'external' == 'true' ) . 'target="_blank"><i class="btn-'. $type .'"></i>' . do_shortcode($content) . '</a>';
}
add_shortcode('button', 'button');

Upvotes: 0

Views: 123

Answers (1)

Rikesh
Rikesh

Reputation: 26431

Keep it simple. Do your operation, assign to variable & use it, as below.

$target = "_self";
if($external == 'true' ){
   $target = "_blank";
}
return '<a href="/'. $link .'" class="btn"  target="' . $target . '"><i class="btn-'. $type .'"></i>' . do_shortcode($content) . '</a>';

Upvotes: 3

Related Questions