Imagine this basic shortcode in your functions.php file:

function div_shortcode( $atts ) {
   return '<div class="div"></div>';
}

add_shortcode('div', 'div_shortcode');

So every [div] in WordPress editor gives <div class="div"></div>.

Everything looks and works fine, right?

Now here comes the trouble.

I need to use my shortcode in PHP file (not putting it via WP Text Editor).

Luckily there’s a function for that:

<?php do_shortcode('[div]'); ?> 

But, wait, it shows… nothing?

<?php do_shortcode(''); ?> 

Even this one doesn’t work.

Why is that happening? Any ideas?

1
1

The shortcode functions just return a value, you need to echo or assign it to something.

<?php echo do_shortcode('[div]'); ?>

Leave a Reply

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