Wp_redirect and sending variables

How to send some variables with wp_redirect() from function.php file in my theme folder?

if ( $post_id ) {
    $variable_to_send = '1';
    wp_redirect( home_url(), $variable_to_send );
    exit;
}

And on homepage I will catch the variable in if-else condition to show some confirmation or not depending if $variable_to_send = ‘1’ or not.

How to do that in WordPress?

2

I’m afraid that you can’t do it this way.

wp_redirect is a fancy way to send header Location and the second argument of this function is request status, and not custom variable. (404, 301, 302, and so on).

You can send some variables as get parameters. So you can do something like this:

if ( $post_id ) {
        $variable_to_send = '1';
        wp_redirect( home_url() .'?my_variable=".$variable_to_send );
        exit;
}

Then you can use these variables as $_GET["my_variable'] or register it as custom get variable.

Leave a Comment