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

plugin development - get_queried_object_id combined with wp_redirect gets wrong id

programmeradmin0浏览0评论

I have the following snippet of code that I am experimenting to restrict access to certain wordpress pages through a plugin.

It works... almost. The browser is redirected to the login page, but the query string is wrong. The query string has the id and slug for the login page and not the page it was redirected from. I want to use this info to redirect back to the page that sent the user to the login page.

add_action( 'template_redirect', 'redirect_to_login_page' );
function redirect_to_login_page() {
    if ( is_user_logged_in() || is_page('login') ) return;

    $postId = get_queried_object_id();
    $slug = get_queried_object()->post_name;
    wp_redirect( '/login?r='.$postId.'&s='.$slug, 302 ); 
    exit;
}

Can anyone tell me why this does not give me the id and slug from the original page?

发布评论

评论列表(0)

  1. 暂无评论