注意,当post更新时,WordPress会从/wp-admin/post.php
到/wp-admin/post.php?post=41&action=edit
这意味着它会加载两次。因此,在打印内容之前,将跳过转换状态挂钩上的操作。
Solution:-
<在过渡挂钩回调函数存储(post meta)中显示js弹出窗口的一些标志,将新函数绑定到
admin_head
挂钩检查是否有标志,显示弹出和重置标志示例:-
//Update post meta to display alert
function add_js_for_pending($post) {
update_post_meta($post->ID, \'trigger_notice\', TRUE);
}
add_action( \'draft_to_pending\', \'add_js_for_pending\' );
add_action( \'auto-draft_to_pending\', \'add_js_for_pending\' );
add_action(\'new_to_pending\', \'add_js_for_pending\');
//JS alert on submit product
function notify_me_for_pending() {
global $post;
$current_screen = get_current_screen();
//Check if we need to display alert
if ($current_screen->base == \'post\' && get_post_meta($post->ID, \'trigger_notice\', TRUE)) {
$notice = __(\'Thank you for your submission. Your product will be available for purchase as soon as we have approved it!\', \'pressive-child\'); ?>
<script type="text/javascript">
<?php echo \'alert("\'.$notice.\'");\'; ?>
</script><?php
delete_post_meta($post->ID, \'trigger_notice\'); //Alert is done now remove it.
}
}
add_action(\'admin_head\', \'notify_me_for_pending\');