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.

I'm trying to use shortcode in my page, I'm starting with a simple test.

In functions.php:

function HelloWorldShortcode() {
    return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');

In index.php:

[helloworld]

This doesn't produce <p>Hello World!</p>. Instead I just get the text [helloworld].

How can I get shortcodes to work in a PHP document?

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

Referering to the Shortcodes API you can do

echo do_shortcode('[helloworld]');

if your shortcode not in post. Or you can do

echo apply_filters('the_content', '[helloworld]');

but this will also bring other filters to work on your shorcode return html.

share|improve this answer
add comment

Use

echo do_shortcode( '[helloworld]' );

if you're going to use it in your template / PHP code.

share|improve this answer
add comment

Shortcodes are parsed through the function do_shortcode(). In some cases this function is applied as a callback automatically, for example when you are using the_content().

To invoke the shortcode parser use something like this:

echo do_shortcode( 'I want to say: [helloworld]' );

But I would not use a shortcode like this, calling the handler without do_shortcode() is faster and easier to read.

share|improve this answer
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.