WordPress表单未提交 [英] Wordpress form is not submitting

查看:85
本文介绍了WordPress表单未提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是WordPress的新手,我已经在wordpress主题中创建了几个php文件。

Hello I am very new to WordPress on my requirement I have created a couple of php files in wordpress theme. Where detailsform.php consists

<form method="post" name="details" action="customerdetails.php">

其中,单击提交按钮后,表单必须重定向到php中的customerdetails.php,但工作正常,但在wordpress中,它给出404错误(找不到页面),我将所有新的php文件都保留在现有主题文件夹中。

where after clicking submit button the form has to redirect to customerdetails.php in php it is working fine but in wordpress it is giving 404 error(page not found) I kept all new php files in the existing theme folder.

请建议我,这正在浪费我的时间。

Please suggest me it is killing my time.

推荐答案

由于wordpress有其自己的特定方式来处理ajax调用,因此wordpress也具有通过表单提交来发布http请求的方式。在 wordpress上的抄本中,有关于此的更多详细信息。

As wordpress has its very own specific way to handle ajax calls, wordpress has it too for post http request by form submitting. In the codex on wordpress there is more details about that.

在简要说明中,将您的functions.php文件用于wordpress主题:

In a brief explanation, using your functions.php file on your wordpress theme:

第一件事在 form 标记的action属性中设置需要执行的操作,指向以下网址:

The first thing you need to do is set in your action attribute from your form tag, point the following url:

< form action = http://www.example.com/wp-admin/admin-post.php method = post>
< input type = hidden name = action value = my_handler_function>
< / form>

一旦指出,您需要添加带有名称属性隐藏输入操作和一个值属性,用于指定操作名称。然后,您需要在functions.php文件上构建处理程序函数。这是您编写所需代码的地方,该代码用于处理全局 $ _ POST $ _ REQUEST 变量将收到的数据。

Once pointed, you need to add a hidden input with the name attribute action and a value attribute specifying the name of your action. Then, you need to build your handler function on your functions.php file. This is where your going to write the code you need for treat the data you will received by the global $_POST or $_REQUEST variable.

function my_handler_function() {
  var_dump($_REQUEST);
  die();
//request handlers should die() when they complete their task
}

下一步是使用动作钩子 admin_post_nopriv _ admin_post _ 将此功能绑定到表单。两者之间的区别在于表格的放置位置。如果您的表单是针对wordpress管理员的自定义功能,则该表单是私有的,您可以使用 admin_post 钩子操作。如果此表单是您公开内容的一部分,则使用 admin_post_nopriv _ ,如以下示例所示:

Next step is to bound this function to your form by using the action hook admin_post_nopriv_ or admin_post_. The difference between these, is where your form is placed. If your form is for a custom functionality for the admin of wordpress then is private and you use admin_post hook action. If this form is part of your public content then use the admin_post_nopriv_ like in the following example:

add_action( 'admin_post_nopriv_my_handler_function', 'my_handler_function' );

如上面的代码示例所示,您需要调用 add_action 函数。在第一个参数中,需要将wordpress提供的动作挂钩与以 action 形式在隐藏输入中指定的动作值结合使用,例如 admin_post_nopriv_ $ action_value 。在第二个参数中,您需要放置在functions.php文件上构建的函数名称。两者都是强制性的。

As it is show in the code example above, you need to call the add_action function. In the first parameter, It is needed to pass the action hook provide by wordpress combined with the action value specify in the hidden input named action in the form, like admin_post_nopriv_$action_value. In the second parameter, you need to placed the function name you build on you functions.php file. Both are mandatory.

为了约定,通常将函数处理程序的名称设置为与操作输入的值相同,以避免误解并获得更多

For matters of conventions, generally, the name of the function handler is set it as same as the value of the action input to avoid misunderstandings and gain more readability.

一旦将所有内容放在一起,您要做的就是测试您的代码。

Onced everything is put it all together, all you have to do is test your code.

祝您编程愉快!

PD:如果您想了解有关wordpress的此过程,请查看 wp-admin / admin-post.php 文件,但甚至不敢修改它。

PD: If you want to clarify about this procedure of wordpress, please take a look in the wp-admin/admin-post.php file, but don't even dare to modify it.

这篇关于WordPress表单未提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆