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'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.
share|improve this question
    
This question is way to broad to get a reasonable answer. Please specify -in your question- what you want to do. –  Camil Staps Mar 1 '13 at 20:42
    
ok, I've added more specifics –  user28249 Mar 1 '13 at 21:21
add comment

2 Answers

up vote 0 down vote accepted

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

<?php
    // redirect if user isn't logged in
    if( ! is_user_logged_in() ) {
        wp_safe_redirect( home_url( '/signup/' ) );
        exit;
    }

    // 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;

        if(
            // 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 ) );
            exit;
        }
    }
?>

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.

share|improve this answer
add comment

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'] ) )
        return;

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

        // community1 or community2 is in the requested pagename path

    endif;

}
add_action( 'parse_query', 'wpa89022_check_user' );
share|improve this answer
    
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 '13 at 15:29
add comment

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.