How to properly encode url with parameters for redirect_to


I´m trying to encode url with parameters to inject in ‘redirect_to’ param of the current url.
This is my code :

if( !is_user_logged_in() ){
    $page = get_post_by_name( 'page', 'custom-log-in' );
    $current_url = rawurlencode(home_url($_SERVER['REQUEST_URI']));
    if( !empty( $page ) ){
         wp_redirect( sprintf( '%1$s?redirect_to=%2$s',
         get_permalink( $page ),
         $current_url )
         wp_redirect( wp_login_url($current_url) 

The problem is the url generated does not convert & so for this following code :

    if( !empty( $_GET['redirect_to'] ) ){
        $redirect_link = $_GET['redirect_to'];

The output cut the url and consider others params ‘&’ like a param for the current page.

Example :

becomes => https://my-site/custom-log-in/?redirect_to=https://my-site/page-1/?h4a-rcos=1&c=123&o=666
so `$_GET['redirect_to']` => 'https://my-site/page-1/?h4a-rcos=1' instead of 'https://my-site/page-1/?h4a-rcos=1&c=123&o=666'

How can I properly encode the redirection url to use it on a page ?

J.BizMai 1 month 0 Answers 10 views 0

Leave an answer