Php Wordpress通过插件处理来自外部网站的表单请求
我想创建一个类似于的端点Php Wordpress通过插件处理来自外部网站的表单请求,php,wordpress,forms,Php,Wordpress,Forms,我想创建一个类似于的端点 http://localhost/wordpress/xxxx/ 无论何时调用都是在处理POST请求。 所以我的情况是: 站点A Web表单->用户提交数据-> Wordpress(http://localhost/wordpress/xxxx/)->我正在解析和处理请求 如何在不直接调用插件文件的情况下实现它? 似乎检测“xxxx”是否用作url的一部分的最简单方法是使用parse_请求: add_action('parse_request', 'xxxx_par
http://localhost/wordpress/xxxx/
无论何时调用都是在处理POST请求。
所以我的情况是:
站点A Web表单->用户提交数据->
Wordpress(http://localhost/wordpress/xxxx/
)->我正在解析和处理请求
如何在不直接调用插件文件的情况下实现它?
似乎检测“xxxx”是否用作url的一部分的最简单方法是使用parse_请求:
add_action('parse_request', 'xxxx_parse_request');
function xxxx_parse_request($wp) {
if (array_key_exists('xxxx', $wp->query_vars)) {
$location = plugin_dir_url(__FILE__).'includes/issuer_json.php';
// process the request.
wp_redirect($location);
}
}
但它只适用于像wordpress?xxxx=1这样的请求,而不适用于wordpress/xxxx。我认为它会对我有用,于是我创建了一个测试表单来检查它。注意,我将表单的操作指定为http://localhost/wordpress/?xxxx=1
并指定了一个隐藏的输入来测试数据的传递
<form action="http://wordpress/?xxxx=1">
<input type="hidden" name="test" value="tes1"/>
<input type="submit" />
</form>
每当我提交表单时,我都会被重定向到url类似的页面:http://wordpress/?test=tes1
因此表单重写了默认操作http://wordpress/?xxxx=1
我不知道如何处理这个问题。任何帮助都将不胜感激。我觉得您需要使用WP admin ajax处理程序 基本上,您可以在插件中创建以下内容:
add_action( 'wp_ajax_my_plugin_function', 'my_plugin_function' );
function my_plugin_function() {
// Handle request then generate response using WP_Ajax_Response
echo "You reached my plugin function";
// important!
exit();
}
$.ajax({
type: "POST",
url: "http://mydomain.com/wp-admin/admin-ajax.php",
data: {'action' : 'my_plugin_function, 'myvar':'another_var'},
error: function(jqXHR, textStatus, errorThrown){
console.error("The following error occured: " + textStatus, errorThrown);
},
success: function(data) {
console.log("Hooray, it worked!");
}
});
然后,在ajax或表单中,使用一个名为action的POST参数(或隐藏输入)将操作发布到其中,该参数的值为“my_plugin_function”
我不经常直接提交表单,但使用jQuery AJAX会有如下效果:
add_action( 'wp_ajax_my_plugin_function', 'my_plugin_function' );
function my_plugin_function() {
// Handle request then generate response using WP_Ajax_Response
echo "You reached my plugin function";
// important!
exit();
}
$.ajax({
type: "POST",
url: "http://mydomain.com/wp-admin/admin-ajax.php",
data: {'action' : 'my_plugin_function, 'myvar':'another_var'},
error: function(jqXHR, textStatus, errorThrown){
console.error("The following error occured: " + textStatus, errorThrown);
},
success: function(data) {
console.log("Hooray, it worked!");
}
});
您可以在中找到更多信息