Wordpress 如何使用 Ajax 显示成功 insert_post() 返回的新数据?
Wordpress how to use Ajax to show new data returned from successful insert_post()?
成功 Ajax 插入条目后,我想查看同一条目的 ID 和 url 是什么,并以模式 window 显示它而不刷新页面
有什么方法可以从 success: function (response) {} 中获取此数据?这是我必须使用 ajax 创建一个新条目的代码,它工作得很好:
<script>
$("#enquiry_email_form").on("submit", function (event) {
event.preventDefault();
var form= $(this);
var ajaxurl = form.data("url");
var detail_info = {
post_title: form.find("#post_title").val(),
post_description: form.find("#post_description").val()
}
if(detail_info.post_title === "" || detail_info.post_description === "") {
alert("Fields cannot be blank");
return;
}
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
post_details : detail_info,
action: 'save_post_details_form' // this is going to be used inside wordpress functions.php// *esto se utilizará dentro de las functions.php*
},
error: function(error) {
alert("Insert Failed" + error);
},
success: function(response) {
modal.style.display = "block"; * abre la ventana modal*
body.style.position = "static";
body.style.height = "100%";
body.style.overflow = "hidden";
}
});
})
</script>
<button id="btnModal">Abrir modal</button>
<div id="tvesModal" class="modalContainer">
<div class="modal-content">
<span class="close">×</span> <h2>Modal</h2> * Ventana modal mostrar le url y ID generado *
<p><?php ***echo $title_post, $url, $ID*** ?></p>
</div>
</div>
存档funtions.php
function save_enquiry_form_action() {
$post_title = $_POST['post_details']['post_title'];
$post_description = $_POST['post_details']['post_description'];
$args = [
'post_title'=> $post_title,
'post_content'=>$post_description,
'post_status'=> 'publish',
'post_type'=> 'post',
'show_in_rest' => true,
'post_date'=> get_the_date()
];
$is_post_inserted = wp_insert_post($args);
if($is_post_inserted) {
return "success";
} else {
return "failed";
}
}
当您使用 wp_insert_post
Docs 功能时,它将 return post id。
It returns the post ID on success. The value 0 or WP_Error on failure.
首先,您可以启动一个空数组并调用它,比方说,$response
并根据 wp_insert_post
函数的 returned 值填充它。
然后,我们也可以使用 id 来获取固定链接,使用 get_permalink
Docs.
最后,我们可以使用 wp_send_json_success
Docs 函数将该数组发送回客户端。
所以你在 php
端的代码应该是这样的:
function save_enquiry_form_action() {
$response = array(
'error' => '',
'success' => '',
'post_id' => '',
'post_url' => '',
);
$post_title = sanitize_text_field($_POST['post_details']['post_title']);
// Note we could have used 'sanitize_title()' function too!
$post_description = sanitize_textarea_field($_POST['post_details']['post_description']);
$args = array(
'post_title' => $post_title,
'post_content' => $post_description,
'post_status' => 'publish',
'post_type' => 'post',
'show_in_rest' => true,
'post_date' => get_the_date()
);
$post_id = wp_insert_post($args);
if($post_id){
$response['success'] = true;
$response['error'] = false;
$response['id'] = $post_id;
$response['post_url'] = get_permalink($post_id);
}else{
$response['success'] = false;
$response['error'] = true;
}
wp_send_json_success($response);
exit;
}
注:
- 我已经使用
sanitize_text_field
Docs function to sanitize the $_POST['post_details']['post_title']
value and sanitize_textarea_field
Docs 函数来清理 $_POST['post_details']['post_description']
值。
- 当您在客户端收到响应时,您可以检查
$response['success']
和 $response['error']
值。
在javascript那边
如您在以下屏幕截图中所见,数据 return 为 data object
。要访问数据,您可以使用 response.data.success
、response.data.error
、response.data.id
和 response.data.post_url
.
成功 Ajax 插入条目后,我想查看同一条目的 ID 和 url 是什么,并以模式 window 显示它而不刷新页面
有什么方法可以从 success: function (response) {} 中获取此数据?这是我必须使用 ajax 创建一个新条目的代码,它工作得很好:
<script>
$("#enquiry_email_form").on("submit", function (event) {
event.preventDefault();
var form= $(this);
var ajaxurl = form.data("url");
var detail_info = {
post_title: form.find("#post_title").val(),
post_description: form.find("#post_description").val()
}
if(detail_info.post_title === "" || detail_info.post_description === "") {
alert("Fields cannot be blank");
return;
}
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
post_details : detail_info,
action: 'save_post_details_form' // this is going to be used inside wordpress functions.php// *esto se utilizará dentro de las functions.php*
},
error: function(error) {
alert("Insert Failed" + error);
},
success: function(response) {
modal.style.display = "block"; * abre la ventana modal*
body.style.position = "static";
body.style.height = "100%";
body.style.overflow = "hidden";
}
});
})
</script>
<button id="btnModal">Abrir modal</button>
<div id="tvesModal" class="modalContainer">
<div class="modal-content">
<span class="close">×</span> <h2>Modal</h2> * Ventana modal mostrar le url y ID generado *
<p><?php ***echo $title_post, $url, $ID*** ?></p>
</div>
</div>
存档funtions.php
function save_enquiry_form_action() {
$post_title = $_POST['post_details']['post_title'];
$post_description = $_POST['post_details']['post_description'];
$args = [
'post_title'=> $post_title,
'post_content'=>$post_description,
'post_status'=> 'publish',
'post_type'=> 'post',
'show_in_rest' => true,
'post_date'=> get_the_date()
];
$is_post_inserted = wp_insert_post($args);
if($is_post_inserted) {
return "success";
} else {
return "failed";
}
}
当您使用 wp_insert_post
Docs 功能时,它将 return post id。
It returns the post ID on success. The value 0 or WP_Error on failure.
首先,您可以启动一个空数组并调用它,比方说,
$response
并根据wp_insert_post
函数的 returned 值填充它。然后,我们也可以使用 id 来获取固定链接,使用
get_permalink
Docs.最后,我们可以使用
wp_send_json_success
Docs 函数将该数组发送回客户端。
所以你在 php
端的代码应该是这样的:
function save_enquiry_form_action() {
$response = array(
'error' => '',
'success' => '',
'post_id' => '',
'post_url' => '',
);
$post_title = sanitize_text_field($_POST['post_details']['post_title']);
// Note we could have used 'sanitize_title()' function too!
$post_description = sanitize_textarea_field($_POST['post_details']['post_description']);
$args = array(
'post_title' => $post_title,
'post_content' => $post_description,
'post_status' => 'publish',
'post_type' => 'post',
'show_in_rest' => true,
'post_date' => get_the_date()
);
$post_id = wp_insert_post($args);
if($post_id){
$response['success'] = true;
$response['error'] = false;
$response['id'] = $post_id;
$response['post_url'] = get_permalink($post_id);
}else{
$response['success'] = false;
$response['error'] = true;
}
wp_send_json_success($response);
exit;
}
注:
- 我已经使用
sanitize_text_field
Docs function to sanitize the$_POST['post_details']['post_title']
value andsanitize_textarea_field
Docs 函数来清理$_POST['post_details']['post_description']
值。 - 当您在客户端收到响应时,您可以检查
$response['success']
和$response['error']
值。
在javascript那边
如您在以下屏幕截图中所见,数据 return 为 data object
。要访问数据,您可以使用 response.data.success
、response.data.error
、response.data.id
和 response.data.post_url
.