Используя вашу текущую функцию, вы можете просто добавить задержку с помощью setTimeout
и использовать window.location
, что-то вроде этого:
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 posts will be subjected to approval period, it may take 24-48h for approval.', 'themename');
$admin_page = admin_url( 'edit.php' ); // Whichever Admin Page You'd Like ?>
<script type="text/javascript">
alert( <?php echo $notice; ?> );
setTimeout(function(){
window.location( <?php echo $admin_page; ?> );
}, 2500 );
</script>
<?php delete_post_meta( $post->ID, 'trigger_notice' ); //Alert is done now remove it.
}
}
add_action( 'admin_head', 'notify_me_for_pending' );
Вы также можете заменить alert
на confirm
и перенаправить, когда пользователь нажимает Okay
:
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 posts will be subjected to approval period, it may take 24-48h for approval. Click "OK" to be redirected, or click "Cancel" to stay here.', 'themename');
$admin_page = admin_url( 'edit.php' ); // Whichever Admin Page You'd Like ?>
<script type="text/javascript">
if( window.confirm( <?php echo $notice; ?> ) ){
window.location( <?php echo $admin_page; ?> );
}
</script>
<?php delete_post_meta( $post->ID, 'trigger_notice' ); //Alert is done now remove it.
}
}
add_action( 'admin_head', 'notify_me_for_pending' );
Вы также можете использовать встроенную функцию WordPress admin_notice
(хотя на момент написания этого поста вам нужно было собрать решение для Гутенберга, поскольку по умолчанию оно скрывает эти уведомления)
function post_approval_notice(){
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 ) ){ ?>
<div class="notice notice-success">
<p><?php __( 'Thank you for your submission, your post will be subjected to admin approval. Note it may take 24-48 hours for approval. <a href="'. admin_url( 'edit.php' ) .'">Click Here to Return</a>', 'themename' ); ?></p>
</div>
<?php }
}
add_action( 'admin_notices', 'post_approval_notice' );
Или, возможно, вы могли бы даже использовать там автоматическое перенаправление:
function post_approval_notice(){
global $post;
$current_screen = get_current_screen();
$admin_page = admin_url( 'edit.php' ); // Whichever Admin Page You'd Like
//Check if we need to display alert
if( $current_screen->base == 'post' && get_post_meta( $post->ID, 'trigger_notice', TRUE ) ){ ?>
<div class="notice notice-success">
<p><?php __( 'Thank you for your submission, your post will be subjected to admin approval. Note it may take 24-48 hours for approval.', 'themename' ); ?></p>
</div>
<script type="text/javascript">
setTimeout(function(){
window.location( <?php echo $admin_page; ?> );
}, 2500 );
</script>
<?php }
}
add_action( 'admin_notices', 'post_approval_notice' );