最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to submit form in a PHP file in WordPress?

programmeradmin2浏览0评论

I have a simple form in my WordPress footer.php:

<form class="form-inline" role="form" method="post" name="contact" action="sub.php">
    <div class="form-group">
        <label class="sr-only" for="my-name">Your first name</label>
        <input name="my-name" type="text" class="form-control" id="my-name" placeholder="Your first name">
    </div>
    <div class="form-group">
        <label class="sr-only" for="my-email">and your email</label>
        <input name="my-email" type="text" class="form-control" id="my-email" placeholder="and your email">
    </div>
    <input  type="submit" class="btn btn-danger" value="Subscribe!">
</form>

I want to submit it to the file sub.php(which is in same directory), as you can see in the action. But even after trying too many solution I still am not able to get the task done.

Please point me to the simplest solution. Thanks

I have a simple form in my WordPress footer.php:

<form class="form-inline" role="form" method="post" name="contact" action="sub.php">
    <div class="form-group">
        <label class="sr-only" for="my-name">Your first name</label>
        <input name="my-name" type="text" class="form-control" id="my-name" placeholder="Your first name">
    </div>
    <div class="form-group">
        <label class="sr-only" for="my-email">and your email</label>
        <input name="my-email" type="text" class="form-control" id="my-email" placeholder="and your email">
    </div>
    <input  type="submit" class="btn btn-danger" value="Subscribe!">
</form>

I want to submit it to the file sub.php(which is in same directory), as you can see in the action. But even after trying too many solution I still am not able to get the task done.

Please point me to the simplest solution. Thanks

Share Improve this question edited Mar 25, 2020 at 2:45 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Jan 4, 2020 at 17:48 thirteen4054thirteen4054 1114 bronze badges 4
  • I see you tagged your question wordpress, is this question about the premium hosting service wordpress? Or was this tagged incorrectly? If it is, then the question would be closed as offtopic and you'll be directed to use the wordpress premium support and support forums – Tom J Nowell Commented Jan 4, 2020 at 18:46
  • should I use wordpress? – thirteen4054 Commented Jan 4, 2020 at 18:47
  • you should use wordpress if you're on a wordpress hosted site ( then use the support channels ), and you should use wordpress if your question is about the wordpress site. Otherwise you should use neither, it just confuses people who might be curious about your question – Tom J Nowell Commented Jan 4, 2020 at 18:49
  • ok. I see an edit has been made to correct my mistake. I will keep it in mind next time. – thirteen4054 Commented Jan 4, 2020 at 18:55
Add a comment  | 

1 Answer 1

Reset to default 1

I want to submit it to the file sub.php(which is in same directory)

Don't do this. You should not have standalone PHP files that get queried via forms or AJAX etc, WordPress handles all the requests. By having a standalone file, you open a can of worms of security issues, and other problems ( you now have to bootstrap WP in your sub.php to use WP functions, which you shouldn't need to do if you did things correctly to begin with ).

Also keep in mind that you could easily just do include( 'sub.php' ) in the form handling code, you don't have to completely rewrite the entire thing, just make sure it isn't possible to call it directly.

So:

  • Use the REST API if you want to talk to your site with javascript, the register_endpoint function is all you need for this
  • Use the same page you're on, and an empty action to handle forms. This handler can be in the same template before any output happens, it could be in functions.php or a plugin if it's redirecting

For example, lets say I have this form in my themes template, and if I enter a secret word I get a surprise here I can enter a word:

<form method="post" action="">
    What's the Secret Word? <input type="text" name="toms_secret_word" />
    <input type="submit" value="Submit"/>
</form>

I now have a form that submits to the same page, with a value I can check for, e.g.:

if ( isset( $_POST['toms_secret_word'] ) ) {
    if ( $_POST['toms_secret_word'] === 'open sesame' ) {
        echo "Correct!";
    } else {
        echo "Incorrect! Try again"
        // display the form
    }
} else {
    //... display the form
}

I'd advise moving the form into a file, that way you can do get_template_part( 'secretwordform' ); and have a secretwordform.php, and even a secretwordform-success.php and secretwordform-incorrect.php.

As a bonus, you can use hidden inputs and have multi-page forms. This way you have a hidden input saying which page is next, and hidden inputs for the items on other pages.

发布评论

评论列表(0)

  1. 暂无评论