错误日志是怎么写的?这就是form-process.php
? 如果是这样,那么问题可能是wp_insert_post()
未定义,因为未加载WordPress核心。
因此WordPress具有AJAX-API. 下面是一个如何在服务器端使用该API的示例:
add_action( \'admin_ajax_your_form_action\', \'wpse_126886_ajax_handler\' );
function wpse_126886_ajax_handler() {
// maybe check some permissions here, depending on your app
if ( ! current_user_can( \'edit_posts\' ) )
exit;
$post_data = array();
//handle your form data here by accessing $_POST
$new_post_ID = wp_insert_post( $post_data );
// send some information back to the javascipt handler
$response = array(
\'status\' => \'200\',
\'message\' => \'OK\',
\'new_post_ID\' => $new_post_ID
);
// normally, the script expects a json respone
header( \'Content-Type: application/json; charset=utf-8\' );
echo json_encode( $response );
exit; // important
}
The
your_form_action
slug是回调的»触发器«。您必须将此slug附加到名为
action
. 跟上
.serialize()
我建议将此段代码作为隐藏输入传递到公式中:
<input type="hidden" name="action" value="your_form_slug" />
最后,您必须使用
wp-admin/admin-ajax.php
AJAX请求的URL:
$.ajax({
type: "POST",
url: "http://www.mysite.co.uk/wp-admin/admin-ajax.php",
data: dataString,
error: function(jqXHR, textStatus, errorThrown){
console.error("The following error occured: " + textStatus, errorThrown);
},
success: function(data) {
console.log("Hooray, it worked!");
}
});