custom form submission

时间:2020-01-06 作者:George Makroglou

我对自定义wordpress页面有点陌生,我购买了一个HTML主题,并成功地将其转换为wordpress网站。问题是,有一个联系方式,我想使用,我已经按照法典指示使用管理职位。php文件,但表单未提交。

下面是我页脚中的表格。php:

<form id="contact-form" method="POST" action="http://tvans.gr/wp-admin/admin-post.php">
    <input type="hidden" name="action" value="send_form"/>
    <input type="hidden" name="custom_nonce" value="<?php echo $custom_form_nonce ?>"/>

    <input class="form-control form-text" id="name" type="text" name="name" value="" placeholder="Please Enter Name" required />
    <input class="form-control form-text" id="email" type="email" name="email" value="" placeholder="Please Enter Email" required />
    <textarea class="form-control form-text" id="message" name="message" placeholder="Message" rows="4" required></textarea>
    <input type="submit" class="btn-1" id="submit" name="submit" value="SUBMIT">
    <input type="reset" class="btn-1 distab-cell-middle cancel" name="clear" value="RESET">
</form>

这是我的功能。php文件:

function prefix_admin_send_form(){
    echo $_POST[\'name\'];
    print($_POST[\'email\']);
    exit;
}

add_action(\'admin_post_send_form\', \'prefix_admin_send_form\');
函数中的数据。php是为了测试目的,但我仍然没有得到任何回应。是我做错了什么,还是我错过了什么?提前感谢您的帮助。

2 个回复
最合适的回答,由SO网友:Arturo Gallegos 整理而成

你不能使用管理员帖子。php直接在您的表单中,您需要实现一个ajax请求。

https://codex.wordpress.org/AJAX_in_Plugins

要发送电子邮件,请使用wp\\u mail功能https://developer.wordpress.org/reference/functions/wp_mail/

如果您在接收POST参数时遇到问题,请查看此答案WordPress AJAX with Axios

SO网友:Chetan Vaghela

您正在使用admin_post 行动,所以你必须使用nopriv 如果用户未登录,也要执行操作。目前,您没有收到任何响应,因为用户需要验证管理post操作。nopriv用于在前端为未经身份验证的用户处理AJAX请求。

Footer.php :

<form id="contact-form" method="POST" action="<?php echo admin_url( \'admin-post.php\' ); ?>">
    <input type="hidden" name="action" value="send_form"/>
    <input type="hidden" name="custom_nonce" value="<?php echo $custom_form_nonce ?>"/>

    <input class="form-control form-text" id="name" type="text" name="name" value="" placeholder="Please Enter Name" required />
    <input class="form-control form-text" id="email" type="email" name="email" value="" placeholder="Please Enter Email" required />
    <textarea class="form-control form-text" id="message" name="message" placeholder="Message" rows="4" required></textarea>
    <input type="submit" class="btn-1" id="submit" name="submit" value="SUBMIT">
    <input type="reset" class="btn-1 distab-cell-middle cancel" name="clear" value="RESET">
</form>

functions.php

function prefix_admin_send_form(){
    echo $_POST[\'name\'];
    print($_POST[\'email\']);
    exit;
}

add_action(\'admin_post_send_form\', \'prefix_admin_send_form\');
add_action(\'admin_post_nopriv_send_form\', \'prefix_admin_send_form\' ); // You nedd to this action

相关推荐

Dealing with html forms

I want to work with html form but i cant understand the Action method and when we need to use it.For example found this code on w3schools:<form action=\"action_page.php\"> First name:<br> <input type=\"text\" value=\"Mickey\">&#