我添加了一個(gè)操作掛鉤,以便在保存帖子后我會(huì)將帖子的信息存儲(chǔ)到會(huì)話變量中。
在我的 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 );
我還創(chuàng)建了另一個(gè)函數(shù),用于檢查會(huì)話中存儲(chǔ)的變量并檢查 post_id 是否已定義,然后我將繼續(xù)顯示帶有消息的 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 } }
在文件末尾我調(diào)用函數(shù):check_new_post_saved();
在我嘗試在 WordPress 中創(chuàng)建并保存帖子后 - 它保存正確,但是當(dāng)我在開發(fā)工具中檢查會(huì)話存儲(chǔ)時(shí),我沒有看到任何變量。我不確定我做錯(cuò)了什么。