我正在添加一個動作掛鉤,以便在我保存帖子后,我會將帖子的資訊存盤到會話變數中。
在我的 php 檔案的開頭,我添加了:session_start()
然后我有:
function save_post_in_session( $post_ID, $post ) {
$_SESSION['post_id'] = $post_ID;
$_SESSION['post_title'] = $post->post_title;
}
add_action( 'created_post', 'save_post_in_session', 10, 2 );
我還創建了另一個函式,用于檢查會話中存盤的變數并檢查是否定義了 post_id,然后我將繼續顯示帶有如下訊息的 div:
function check_new_post_saved() {
if( isset( $_SESSION['post_id'] ) ) {
?>
<div class='custom-alert' id='comment_custom_alert'>
<div class='alert-success'>
<button type='button' onclick='this.parentNode.parentNode.remove()' class='close'>×</button>
<strong>Success!</strong> Your post has been saved successfully.
</div>
</div>
<?php
}
}
在檔案末尾我呼叫函式:check_new_post_saved();
在我嘗試在 wordpress 中創建和保存帖子后 - 它可以正確保存,但是當我在我的 devtools 中檢查我的會話存盤時,我沒有看到任何變數。我不確定我做錯了什么。
uj5u.com熱心網友回復:
保存帖子后運行的掛鉤名為wp_insert_post
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/458990.html
上一篇:注銷禁用的用戶-Laravel8
