WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Contact Form 7 and can display the form by putting the following in the content text field [contact-form-7 id="453" title="Contact form 1"].

Now I need to display the form outside the loop.

How can I execute a short code from within my template code?

I found some suggestions to execute shortcode outside the loop:

<?php echo do_shortcode('[contact-form-7 id="453" title="Contact form 1"]') ?>

This solution works fine :)

share|improve this question
up vote 2 down vote accepted

do_shortcode should do the trick. I don't think you need the 'echo' before it though, I have never done so myself and never had any problems.

share|improve this answer
Yes it did. I just had butterfingers when implementing it. It works now :) – Steven Apr 13 '12 at 7:26
@Steven, glad I Could help :3 – mystline Apr 13 '12 at 7:57
  1. Dig into the Contact Form 7 plugin;
  2. Find occurrence of add_shortcode( 'contact-form-7', 'some_contact_form_function' ) function;
  3. Learn some_contact_form_function function parameters;
  4. Call it from your code.
share|improve this answer
I know I can dig into the code. But I know I have seen somewhere that shortcode can be executed outside the loop. I just don't remember how. And it's a lot easier than digging around somebody elses' code. – Steven Apr 13 '12 at 7:14
All you need is to find add_shortcode( 'contact-form-7', ... ) or add_filter( 'the_content', '...' ) calls. If you find it, you will see which function is called to process this shortcode. If you know which function processes it, you can call it directly whenever you need it. – Eugene Manuilov Apr 13 '12 at 7:17

Just wanted to add this to help people like me who was trying to figure out how to end or close the shortcode...

The answer is here:


For example:

// In case there is opening and closing shortcode
echo do_shortcode('[iscorrect]'.$text_to_be_wrapped_in_shortcode.'[/iscorrect]');
share|improve this answer

Your Answer


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.