I have a page that has multiple shortcodes on it. I would like to have the content from each shortcode wrapped with an HTML tag. I have the following code that almost works but it wraps all of the shortcodes in one div.
<?php $sidebarBlocks = get_post_meta($post->ID, "page_sidebarhubspot", true); ?>
<?php echo do_shortcode('<div>'.$sidebarBlocks.'</div>'); ?>
This outputs..
<div>
<p>Content from shortcode1</p>
<p>Content from shortcode2</p>
<p>Content from shortcode3</p>
</div>
What I want is this..
<div>
<p>Content from shortcode1</p>
</div>
<div>
<p>Content from shortcode2</p>
</div>
<div>
<p>Content from shortcode3</p>
</div>
How can I accomplish this? Thanks!
That’s a good question – I don’t think it’s currently possible without hacking around. Here’s my go at that:
So what happens here is that on
init
we copy all of the registered shortcodes and overwrite their callback function with our own function.That function on the other hands when called returns an opening div tag, followed by the output of the original callback function and then a closing div tag.
If you want to override the shortcodes just for that
do_shortcode
call, you can do something like this:And in your code do this:
Of course if you use the second method, don’t forget to remove the line
As a bonus in the
my_override_shortcodes()
function you can specify shortcodes that will not be overwritten(the$disabled_tags
array).You can try like this [You can remove the
htmlentities()
, I have used it for illustration purposes , just use plainecho
]OUTPUT :