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

问题描述 投票:0回答:1

我有这个小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');
wordpress redirect post notice
1个回答
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' );
© www.soinside.com 2019 - 2024. All rights reserved.