如何将表单发布到WordPress的另一个PHP文件? [英] how to post form to another php file in wordpress?
问题描述
所以我的应用程序主题结构是
-wp-content
-主题
-cool
-templates
-landing.php
- post-landing.php
我在landing.php中有一个表单,我想在post-landing.php中处理POST请求,我有这个请求,
form action =wp-content / themes / cool / templates / post-landing.php
但是当我发布一个post请求时,它说 404。
还有一种方法:如果你想异步管理它,你可以像这样创建一个AJAX调用(我通常将它放在我的< theme> / inc
文件夹中,所以它的自动加载):
ADD_ACTION( 'wp_ajax_nopriv_< call_name>', '<作用名称>');
add_action('wp_ajax_< call_name>','<函数名称>');
function< function_name>(){//这里你用$ _POST做了一些事情并返回json}
在您的 functions.php
文件中进行本地化,以便让请求的ajaxUrl始终可用于变量并仅用于您的js文件:
wp_localize_script( '排队-JS-名', '的varName',阵列(
'ajaxUrl'=> admin_url('admin-ajax.php')
));
然后你可以在入队的JS文件中调用它:
'pre>
jQuery.ajax({
网址:varName.ajaxUrl,
型: '后',
数据:currentDataToSubmit,
成功:函数(响应){
//管理响应
}
});
就是这样!希望它有用!
如果第一部分出现问题,请告诉我,我也可以学习!
So my app theme structure is
-wp-content
-themes
-cool
-templates
-landing.php
- post-landing.php
I have a form in landing.php and I want to handle POST request in post-landing.php for which I have,
form action="wp-content/themes/cool/templates/post-landing.php"
But when I make a post request it says 404.
Here's another way: in case you want to manage that asynchronously, you can create an AJAX call like this(I usually place it in my <theme>/inc
folder, so it's automatically loaded):
add_action( 'wp_ajax_nopriv_<call_name>', '<function name>' );
add_action( 'wp_ajax_<call_name>', '<function name>' );
function <function_name>() {//here you do things with $_POST and return json}
Localize it in your functions.php
file in order to have your ajaxUrl for the request always available in a variable and only for your js file:
wp_localize_script( 'enqueued-js-name', 'varName', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' )
));
And then you can call it like this in your enqueued JS file:
jQuery.ajax({
url: varName.ajaxUrl,
type: 'post',
data: currentDataToSubmit,
success: function(response){
//manage the response
}
});
And that's it! hope it was useful!
If there's something wrong with the first part, let me know, so I can learn too!
这篇关于如何将表单发布到WordPress的另一个PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!