Use 0
and 1
as shortcode values and use that show or hide the HTML attribute.
Example with an element input
and the attribute required
:
function input_shortcode( $atts )
{
$values = shortcode_atts(
array (
'type' => 'text',
'value' => '',
'name' => '',
'id' => '',
'required' => 0,
),
$atts
);
$values = array_map( 'esc_attr', $values );
return sprintf(
'<input type="%1$s"%2$s%3$s%4$s%5$s>',
$values[ 'type' ], // 1
'' === $values[ 'name' ] ? '' : ' ' . $values[ 'name' ], // 2
'' === $values[ 'value' ] ? '' : ' ' . $values[ 'value' ], // 3
'' === $values[ 'id' ] ? '' : ' ' . $values[ 'id' ], // 4
0 === $values[ 'required' ] ? '' : ' required' // 5
);
}
Please don’t use extract()
. Ever.