Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to redirect non logged in users to page id = 2 which have registration form.

    function checkLogged()
{
    $pg = get_permalink();
    if (!is_user_logged_in() && !is_front_page() && $pg != home_url('/?page_id=2'))
    {
        wp_redirect(home_url('/?page_id=2'));
        exit;
    }
}
add_action('wp_head', 'checkLogged');

Is this code correct ? I am getting

This webpage has a redirect loop The webpage at http://abc.com/dsdd_wordpress/?page_id=2 has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Use following code instead of above code :

function checkLogged()
{

if (!is_user_logged_in() && !is_front_page() && 2 != get_queried_object_id())
{
    wp_redirect(home_url('/?page_id=2'));
    exit;
}
}
add_action('wp_head', 'checkLogged');
share|improve this answer
    
Thanks It works :) –  Ajay Patel Feb 27 '13 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.