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 :)


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.

  • 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
  • I know it's a very old question but did you manage to use this with on_sent_ok: "$(".form-div").fadeOut();" somehow this doesn'r work outside the loop, but i does send the form
    – alex
    Oct 10 '17 at 14:48
  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.
  • 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. 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]');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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