I'm searching for an action hook which I can use to control access to certain parts of my Wordpress site. I want to write some custom PHP which will check certain user attributes and decide if they can view a particular part of the site (ie only users with certain attributes can view pages which fall under particular url hierarchies).

More specifically:

  • My user profile has two extra flags: i) member of community 1 and ii) i) member of community 2.
  • Community one consists of a load of content which falls under a page with slug "community1"
  • Every time a use tries to access the page with slug community1 or any of it's sub-pages (eg community1/about), I want to check that they are a. logged in and b. have the community1 flag set in their profile. If they do then I allow the access, if not I redirect to page explaining that they need to sign up for access.
  • The same for community2.
  • This question is way to broad to get a reasonable answer. Please specify -in your question- what you want to do.
    – user26607
    Mar 1, 2013 at 20:42
  • ok, I've added more specifics
    – user28249
    Mar 1, 2013 at 21:21

2 Answers 2


Put this at the very top of your page.php file, before the get_header() call:

    // redirect if user isn't logged in
    if( ! is_user_logged_in() ) {
        wp_safe_redirect( home_url( '/signup/' ) );

    // supply IDs of both parent pages
    $keys = array( '21', '23' );

    // check if user can access page
    foreach( $keys as $key )
        // get parent page object
        $parent = get_page( $key );

        // add IDs of parent and children to array
        $pageids = array();
        $objs = get_pages( array( 'child_of' => $parent->ID ) );
        foreach( $objs as $obj ) $pageids[] = $obj->ID;
        $pageids[] = $key;

            // check if current page is restricted area
            in_array( get_queried_object_id(), $pageids )
            // and check if current user has appropriate meta
            && '1' !== get_user_meta( get_current_user_id(), $parent->post_name, true )
        ) {
            // redirect to signup page if conditions aren't met
            wp_safe_redirect( home_url( '/signup/?for=' . $parent->post_name ) );

Notes: (a) you'll have to supply the correct page IDs for the $keys array on line 9, and (b) this code expects users to have user meta with key community1 (and/or community2), and value 1 if they have access.


Diggy's answer will get you most of the way there, however, if you want to do all of this before the database is queried and template is loaded, you can hook parse_query and look at the requested page:

function wpa89022_check_user( $query ){

    // not a page
    if( ! isset( $query->query['pagename'] ) )

    if( false !== strpos( $query->query['pagename'], 'community1' ) 
    || false !== strpos( $query->query['pagename'], 'community2' ) ):

        // community1 or community2 is in the requested pagename path


add_action( 'parse_query', 'wpa89022_check_user' );
  • Thanks. Meanwhile, I also found a hook called 'pre_get_posts' which seems to do what I want. Which runs first between this and parse_query?
    – user28249
    Mar 4, 2013 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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