我具有使用小型JS的功能,可以在我们的用户单击提交以供审阅后通知他们。
警报完成后,如何将它们重定向到后管理区域。
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');
答案 0 :(得分:0)
使用您当前的功能,您可以使用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
功能(尽管在撰写本文时,您需要为古腾堡(Gutenberg)破解一个解决方案,因为默认情况下它会隐藏这些通知)
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' );