I have this function with small JS to notice our users after their clicking submit for review.
How can i redirect them after alert is done to the post admin area.
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'); ?>
<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');
Using your current function, you could just add a delay with setTimeout
and use window.location
, something like this perhaps:
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' );
You could also replace your alert
with a confirm
, and redirect when the user clicks 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' );
You could also make use of WordPress' native admin_notice
functionality (though at the time of this post you'll need to hack together a solution for Gutenberg since by default it hides these notices)
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' );
Or you could potentially even work the automatic redirect in there as well:
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' );