17Apr

WordPress shortcode showing at the top of the page bug

I had a little issue building my shortcode. It worked fine but if I used it into the CMS that broke the page. I missed a couple of important lines.

Why is my WordPress shortcode showing at the top of the page?

That’s what I asked myself, it crashed the full website, oops (personal project, no biggies). When not crashing it was flashing quickly when the CMS page loaded and in the front-end it showed at the top after the h1 title.

If you create a WordPress shortcode, you need to define the shortcode in your functions.php. Mine was a simple shortcode without parameters. It imports a block of HTML and PHP loop to show specific content.

So in function.php, here is a simple embed shortcode of [myCustomEmbedShortcode].

function myCustomEmbedShortcode_function() {
     include dirname( FILE ) . '/template-parts/myCustomEmbedShortcode.php';
}
add_shortcode('myCustomEmbedShortcode', 'myCustomEmbedShortcode_function');

It was buggy because I forgot to add the ob_start(); and return ob_get_clean();

function myCustomEmbedShortcode_function() {
     ob_start();
     include dirname( FILE ) . '/template-parts/myCustomEmbedShortcode.php';
     return ob_get_clean();
}
add_shortcode('myCustomEmbedShortcode', 'myCustomEmbedShortcode_function');

Easy fix. Hope this can help someone without loosing hours.

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments