Apply a Shortcode within a Shortcode

Suppose that you have two shortcodes,

1. the [RED]content[/RED] – will change the color of the text to red.

1. the [DOTTED]content[/DOTTED] – will add dotted underline to the text.

{code}function shortcode_red($atts, $content){
return ‘<span style=”color:red”>’.$content.'</span>’;
}

add_shortcode(‘RED’, ‘shortcode_red’);

function shortcode_dotted($atts, $content){
return ‘<span style=”border-bottom:dotted gray 1px”>’.$content.'</span>’;
}

add_shortcode(‘DOTTED’, ‘shortcode_dotted’);{/code}

 

Adding “[RED]this is red[/RED] [DOTTED]this is dotted[/DOTTED]” to the post content, the result is fine. However, when placing the ” [DOTTED]this is dotted[/DOTTED]” inside [RED][/RED] as in “[RED][DOTTED]this is dotted[/DOTTED][/RED]”, will result to a problem. The [DOTTED] shortcode won’t be executed.

 

To fix this, on the return statement, add do_shortcode() function.

{code}function shortcode_red($atts, $content){
return do_shortcode(‘<span style=”color:red”>’.$content.'</span>’);
}

add_shortcode(‘RED’, ‘shortcode_red’);

function shortcode_dotted($atts, $content){
return do_shortcode(‘<span style=”border-bottom:dotted gray 1px”>’.$content.'</span>’);
}

add_shortcode(‘DOTTED’, ‘shortcode_dotted’);{/code}

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this pageEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *