Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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('[gallery]'); ?> 

Even this one doesn't work.

Why is that happening? Any ideas?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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

<?php echo do_shortcode('[div]'); ?>
share|improve this answer
    
Christ, I've tried it before and nothing. But today it works. Stupid typos! Thank you so much! :) –  Wordpressor Jun 2 '11 at 8:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.