How to make inline-style with Shortcode


@Jude, @Jose

I often create shortcodes where I want to implement custom inline css.

My issue is that if there is is custom css, then I want to add the style"" tag with the css, however, if there is nothing then I do not want to add the style="" tag.

Now, I could add:

add_shortcode( 'shortcode', 'example' );
function example( $atts ) {

$atts = shortcode_atts( array(
	'font-color'	=>	'',
	'bg-color'		=>	'',,
	), $atts, 'shortcode-cta' );

ob_start(); ?>

<div <?php if( ! $atts['font-color'] ) echo 'style="color:' . $atts['font-color'] . '"'?></div>

<?php return ob_get_clean();


However, if I then want to add the 'bg-color' then I can't open the style again since that would through me an error.

So, I have to add the 'bg-color' in as a parameter where I could use the ternary operator.


$bagground_color = $atts['bg-color'] ? "background: " . $atts['bg-color'] : '';

However, what if the style has not even been opened yet, then I will get an error?

Does it make any sense? The examples above is just from the top of my head, so they might not be 100 complete :slight_smile: