如何将其重定向到新标签?

I'm working on redirecting posts to url's and this currently does the job nicely but I cannot work out how to get it to open the url in a new tab.

defined( 'ABSPATH' ) or exit;

function bhrdr2p_redirect_post_to_url() {
    if( !is_singular() ) return;

    global $post;
    $redirect = esc_url( get_post_meta( $post->ID, 'redirect', true ) );
    if( $redirect ) {
        wp_redirect( $redirect, 301 );
        exit;
    }
}
add_action( 'template_redirect', 'bhrdr2p_redirect_post_to_url' );


//redirect any drafted posts
add_action('template_redirect', 'bhrdr2p_rtrash_redirect');
function bhrdr2p_rtrash_redirect(){
    if ( !current_user_can( 'edit_pages' ) ) {
        if (is_404()){
            global $wp_query, $wpdb;
            $page_id = $wpdb->get_var( $wp_query->request );
            $post_status = get_post_status( $page_id );
          $redirect = esc_url( get_post_meta( $page_id, 'redirect', true ) );

            if($post_status == 'draft' && $redirect){
                wp_redirect( $redirect , 301);
                die();
            }
        }
    }
}

you must use the javascript here to redirect in new tab

if($post_status == 'draft' && $redirect){            
 <script>
     window.open( '<?php echo $redirect; ?>', '_blank' );
 </script>
 die();
}

Since PHP is server-side, you can't.

Both the header() function and Wordpress' wp_redirect() do not offer such functionality.


You could try using JavaScript, it might work:

Change the Wordpress redirect:

wp_redirect( $redirect , 301);

To a small JavaScript script.

echo "<script> window.open(".$redirect.", '_blank') </script>";