Shortcode inside shortcode not working in Wordpress?
-
08-10-2019 - |
Question
I need to add shortcodes such as image
, hr
and button
. However, I also needed to remove the wpautop
filter (remove_filter('the_content','wpautop')
) for hr to work properly. Now, to manually set paraghraps I set a shortcode p
to simply convert [p]text[/p]
with <p>text</p>
.
But, inside [p /]
the other shortcodes aren't working. I guess that it modifies the content. Here's my simple function:
function p_func($atts,$content) {
return '<p>'.$content.'</p>';
}
add_shortcode('p','p_func');
I'm getting a bit nervous with this, as my functions.php
seems to be full of hacks already. Any advice?
Martti Laine
Solution
I'm guessing you need to remove wpautop
for your hr
shortcode as it gets wrapped in a paragraph?
If so, just use your shortcode like so;
Some text in my editor. [hr] A bit more text.
See the line break spacing? WordPress will automatically ensure that shortcode 'by itself' won't get wrapped in paragraphs.
I'd strongly advise going down the route you are at the moment - if what I've mentioned above isn't the problem, what is? Let's look at tackling it rather than re-inventing the wheel!
For the record, if you need to apply nested shortcode, from the codex;
If the enclosing shortcode is intended to permit other shortcodes in its output, the handler function can call do_shortcode() recursively:
function caption_shortcode( $atts, $content = null ) { return '<span class="caption">' . do_shortcode($content) . '</span>'; }