How to pass a message using template_redirect

Question

I’m using the following function to redirect a post to another using custom field. It’s working just fine until I added the line#8 to pass an ID so that I can grab it in to the post (to where was redirected), and show a custom message that, you are redirected from that particular post:

function project_do_redirect() {
    if( !is_singular( 'mycpt' ) && !is_single() )
        return;

    global $post;
    $redirect_post_id = get_post_meta( $post->ID, 'redirect', true );
    $redirect_url = $redirect_post_id ? get_permalink( $redirect_post_id ) : false;
    $redirect_url = esc_url( add_query_arg( 'redir_from', $post->ID, $redirect_url ) );
    if( $redirect_post_id ) {
        wp_redirect( $redirect_url, 301 );
        exit;
    }
}
add_action( 'template_redirect', 'project_do_redirect' );

Problem is:
If I was redirected from post #12 to post #13, the ?redir_from= shows 13 instead of 12. So the thing is not actually working as expected.

So, without passing any parameter to the URL, how can I pass a message to the redirected page?

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/how-to-pass-a-message-using-template_redirect/">

How to pass a message using template_redirect</a>
0
, , Mayeenul Islam 2 weeks 0 Answers 10 views 0

Leave an answer