How to create colored textboxes as a mu-plugin?

Hello,
I have created a plugin for wrapping text inside colored divs with shortcodes:

add_shortcode( 'tbred', function($atts, $content = null) { return '<div class="textboxred">' . $content . '</div>'; });
add_shortcode( 'tbgreen', function($atts, $content = null) { return '<div class="textboxgreen">' . $content . '</div>'; });
add_shortcode( 'tbblue', function($atts, $content = null) { return '<div class="textboxblue">' . $content . '</div>'; });

The corresponding divs are placed in Customizer > Additional CSS and it's all working fine.

But I'd like to use only one "main" shortcode instead of the three mentioned here, and add an "id", as in teh following example:

Instead of [textboxred]
I want to have [textbox id="tbred"]

How can I achieve this?
Thank you and kind regards,
Sascha

PS: If you recommend a better way for wrapping text inside a colored box I'd love to hear about too :slight_smile:

  • Nahid

    Hey there sushling !
    Hope you are having a great day!

    Could you please try adding the following custom PHP code instead for the shortcode?

    add_shortcode( 'textbox', function( $atts, $content = null ) {
    
    	$a = shortcode_atts( array (
            'id' => '',
        ), $atts );
    
        $textboxColor = $a[ 'id' ];
    
        if ( $textboxColor == 'red' ) {
    		return '<div class="textboxred">' . $content . '</div>';
    	} elseif ( $textboxColor == 'green' ) {
    		return '<div class="textboxgreen">' . $content . '</div>';
    	} elseif ( $textboxColor == 'blue' ) {
    		return '<div class="textboxblue">' . $content . '</div>';
    	} else {
    		return '<div class="textbox">' . $content . '</div>';
    	}
    } );

    This code will let you use one single shortcode named [textbox], which accepts the three colors (red, green, blue) as IDs, for example, [textbox id="red"].

    Hope this helps. Please let us know if this works for you. Thanks!

    Kind regards,
    Nahid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.