[英]Wordpress how to use Ajax to show new data returned from successful insert_post()?
在成功插入一個條目的 Ajax 后,我想看看同一條目的 ID 和 url 是什么,並在不刷新頁面的情況下以模態 window 顯示它
有什么方法可以從成功中獲取此數據:function(響應){}? 這是我必須使用 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>
存檔功能.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 function 時,它將返回帖子 ID。
它在成功時返回帖子 ID。 失敗時值為 0 或 WP_Error。
首先,您可以啟動一個空數組並調用它,比如說, $response
並根據wp_insert_post
function 的返回值填充它。
然后,我們也可以使用 id 來獲取永久鏈接,使用get_permalink
Docs 。
最后,我們可以使用wp_send_json_success
Docs function 將該數組發送回客戶端。
因此,您在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 function to sanitize the $_POST['post_details']['post_description']
value.$response['success']
和$response['error']
值。javascript側
正如您在以下屏幕截圖中看到的,數據返回為data object
。 要訪問數據,您可以使用response.data.success
、 response.data.error
、 response.data.id
和response.data.post_url
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.