I am trying to create a custom form in wordpress.

Step 1 of the form is html code that collects data and sends it to a php file through the post method, then writes it to the MySQL database and creates Step 2 of the form using php code.

My problem is that I want to include the default wordpress header and footer in Step 2 of the form that wordpress uses in Step 1. Is there a way to do this by including the code of header.php and footer.php in my php script?

I have tried putting this at the top of the script:

<?php get_header(); ?>

but it gives me an undefined function error.

Thanks!

  • 2
    You shouldn't be creating PHP files in your plugin/theme that the browser accesses directly, it's a security risk and greatly complicates development introducing lots of new problems. Everything should go through WordPress – Tom J Nowell Oct 6 '16 at 14:53
  • Then what is the approach to creating a complicated form that accesses the MySQL database in multiple steps to create the form questions and answers? – Stephen Rose Oct 6 '16 at 15:17
  • Why can't you just create a shortcode of the php file and include it in a page by echo do_shortcode('[my-php-code-shortcode-1]');. In that case you will get both header and footer already included by default. – Aniket Sahrawat Oct 6 '16 at 15:22
  • So Step 1 of my form, which is html, calls a php file when the Submit button is clicked. The php file executes and collects the data from Step 1 of the form and writes it to a MySQL database. My original plan was to have more code in the php file to create Step 2 of the form, but you are saying I could embed a shortcode in a wordpress page which references a Step 2 php file? My question is how does the Step 2 php file get the data that was collected in the first php file? It gets it from the MySQL database? – Stephen Rose Oct 6 '16 at 16:16

Firstly, never send the browser directly to a PHP file in your theme or plugin, it's a security hole, and leads to a very fragile setup. For example:

  • The file will work even if the theme is deactivated, and will work for all sites on an install, not just those it's enabled for
  • The file will need to reach up and bootstrap WordPress, leading to longwinded include paths that are fragile, and nonstandard contexts which can confuse plugins
  • Creating the form on the frontend becomes a lot more complex as you need to specify the path of the form handler file, and gets more complex if you have server side validation that needs to know what went wrong to display the form again

So instead:

  • For AJAX, use register_rest_route and the REST API to create custom endpoints that accept the data you need
  • For form handling, use the page you're already on. You don't need a special form handler page, you just need to check if the form was submitted and act accordingly

e.g:

<form method="post">
    <input type="hidden" name="doing_form" value="yes"/>
</form>

Then on the init hook:

add_action( 'init', function() {
    if ( empty( $_POST['doing_form'] ) ){
        return; // we didn't submit the form
    }
    // we did! Do the form handling
    ...
}

Finally, storing your contact forms in a mysql table, I would advise against this. Use a custom post type instead. It'll give you an admin interface, you can use WP_Query instead of raw SQL, and you can import and export. You can even display them on the frontend if you really wanted to with a URL, archive, templates all provided automatically by WordPress.

That's how popular contact form plugins that already do what you're trying to do implement it, e.g.:

  • Ninja Forms
  • Contact Form 7
  • Gravity Forms
  • Jetpack Contact Forms
  • 100's more

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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