WordPress.org

Ready to get started?Download WordPress

Forums

Help needed with is_page() (5 posts)

  1. born4th
    Member
    Posted 2 years ago #

    For some reason I cannot seem to get the is_page function to work. I am trying to make it so if someone is not logged in when they go to a specific page (not the site as a whole) they will be redirected to go login first. Here is my code:

    if ( !is_user_logged_in( ) && is_page(4) ) {
    wp_redirect( "http://www.sample.com", 301 );
    exit;
    }

    When I use the if_user_logged_in by itself it works fine, but when I add the is_page() condition it won't trigger. When I try to use the is_page condition by itself without the logged in function it won't work either. I have been placing the code at the top of the functions.php file right after the opening php tag and right before the first set of comments. Can someone please help me out and tell me what I'm doing wrong. Am I placing the code in the wrong place perhaps? Thank you in advance :-)

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    When I try to use the is_page condition by itself without the logged in function it won't work either.

    Are you sure you have the right page id?

  3. born4th
    Member
    Posted 2 years ago #

    Yes, I installed a plugin that displays the page ID just to make sure also. I've tried with multiple pages to test it out and it won't work for me.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    is_page() has always worked for me, so I can only assume that the issue is due to your placement of the code above.

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    i.e. what else is going on on that page 4?

    any custom queries?
    any plugins which might use custom queries?

    have you tried to reset the queries before your conditional code, for instance with wp_reset_query(); ?
    http://codex.wordpress.org/Function_Reference/wp_reset_query

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags