Wordpress - 单击提交以供审核后重定向用户

Wordpress - Redirect users after click submit for review

我有这个功能,用小 JS 来通知我们的用户点击提交以供审核。

在 post 管理区域完成警报后如何重定向它们。

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');

使用您当前的功能,您可以使用 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 功能(尽管在这个时候 post 您需要为古腾堡拼凑出一个解决方案,因为默认情况下它会隐藏这些通知)

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' );