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 |1 Answer
Reset to default 4Firstly, 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
版权声明:本文标题:Creating a WordPress form with a PHP script and default header 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744971288a2635249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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