Would you like to add your WPForms shortcodes directly to your PHP template? There are many different ways to include your WPForms inside your pages an editor from WordPress. You can embed the form inside a post, page, or widget area on your site. However, did you know you can also add a small PHP snippet to have the shortcode run inside any of the PHP templates in your them? In this tutorial, we’ll show you the steps to use to include the shortcode inside a PHP template.
Creating the form
First, you’ll need to create your form and add the fields you wish to have. Our form will have some basic fields to collect our initial contact information for our newsletter.
If you need help in creating your form, please see this documentation.
Adding the shortcode to a PHP template
It’s very important to note that we never recommend making any changes to any of your theme files without the use of a child theme. If you’d like to learn more about creating a child theme, please see this article from our friends at WPBeginner.
For more information on how to add code to any template file in your theme, please review this documentation.
Since we want our newsletter to appear at the bottom of every page on our site automatically, we’re going to use a child theme and edit the footer.php inside our child theme.
Just before the closing content div of the footer, we’re going to add this code.
<?php echo do_shortcode('[wpforms id="25"]');?>
Just be sure to update the id=”25″ with the form ID number you want to include in the template. If you need any help in how to locate your form ID number, please check out this tutorial.
Now when you load each page of your site, you’ll see the form at the bottom of every page.
As we mentioned, this is just one way. If your theme offers a footer widget, it’s much easier to just add your WPForms shortcode directly to the widget and there’s no need for coding edits or changes using this method.
And that’s all you need! Would you also like to know how to display a shortcode inside an HTML Field inside your form? Take a look at our article on How to Display Shortcodes Inside the HTML Field.
Q: What if I want my form title and description to display?
A: To display your title and form description when embedding the shortcode on a PHP template. You’ll want to use this shortcode instead.
<?php echo do_shortcode('[wpforms id="25" title="true" description="true"]');?>