admin管理员组

文章数量:1406177

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, and 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.

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, and 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.

Share Improve this question edited Nov 25, 2019 at 14:17 Peter Mortensen 2682 silver badges10 bronze badges asked Oct 6, 2016 at 14:11 Stephen RoseStephen Rose 111 gold badge1 silver badge2 bronze badges 4
  • 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 Commented Oct 6, 2016 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 Commented Oct 6, 2016 at 15:17
  • 1 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. – user104130 Commented Oct 6, 2016 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 Commented Oct 6, 2016 at 16:16
Add a comment  | 

1 Answer 1

Reset to default 4

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 long-winded 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 it 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

For example:

<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, for storing your contact forms in a MySQL table, I would advise against this. Use a custom post type instead. It'll give you an administrator 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, and 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

本文标签: Creating a WordPress form with a PHP script and default header