最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

redirect - How to properly encode url with parameters for redirect_to

programmeradmin1浏览0评论

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 )
    );
    }else{
         wp_redirect( wp_login_url($current_url) 
    }
    exit;
 }else{...}

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 :

https://my-site/page-1/?h4a-rcos=1&c=123&o=666
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 ?

发布评论

评论列表(0)

  1. 暂无评论