Creating nested shortcode concept

Surely I’m not the first but after recently being asked by a visitor about nested shortcodes I figure I would fool around with the idea. Adding this snippet to the functions.php of your wordpress theme will let you fool around with the following nested shortcode [link][my_text][/link]. No tricks just pass $content into do_shortcode() and works like a charm.

 

 

add_shortcode("my_text", "my_text");
function my_text() {
return 'nested shortcode';
}
function my_link($atts, $content = null) {
extract(shortcode_atts(array(
"href" => 'http://'
), $atts));
return '<a href="'.$href.'">'.do_shortcode($content).'</a>';
}
add_shortcode("link", "my_link");

 

[link][my_text][/link]

Leave a comment