0

Is there any possibility to redirect visitors to home page, for example, if they aren't allowed to view page's/post's content, using pre_get_posts action?

I'm trying to develop a simplified membership plugin and I want to test if current_user/ current_visitor is allowed to view the content of page/post. For that, I want to retrieve the queried post id from wp_query and test if user/visitor can view the content he asked for. If he can't, I want to redirect him to an error page.

I'm also using classes to encapsulate a little bit data. Here's a piece of code:

class Public {
    public function __construct() {
        add_action('pre_get_posts', array($this, 'try_redirect'));
    }

    private function try_redirect() {
        wp_redirect( home_url() ); // doesn't work
    }
}

If this isn't possible can you bring me another idea? Thanks!

0

you can allow only registered user to see your content by

1.this solution for post show only to login user

class RavsPublic {
    function __construct() {
        add_action('pre_get_posts', array($this, 'try_redirect'));
    }

    function try_redirect( $query ) {
        // not on home page and not login
        if( !is_home() && !is_user_logged_in() ){
            // send them to home page
            wp_redirect( home_url() );
            exit();
            return $query;
        }
        return $query;
    }
}
new RavsPublic();

2)this solution if each post contain a user list ( as post meta ) which can see this post.

//show your content only to registered users
class RavsPublic {
    function __construct() {
        add_action('pre_get_posts', array($this, 'try_redirect'));
    }

    function try_redirect( $query ) {

        global $current_user;
        get_currentuserinfo();

        //current user login name
        $user = $current_user->user_login;

         $allowed_user_list = False;

        //if visiter visiting a post
        if( $query->query_vars->is_single ){

            //retrieve post
            $args=array(
                'name' => $query->query_vars->name,
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => 1
            );
            $my_posts = get_posts( $args );

            //retrieve post meta which contain allowed user list
            $allowed_user_list = get_post_meta( $my_posts[0]->ID, 'allowed_user_list');

            //check if user exist in allowed user list
            $is_user_allowed = in_array( $user, $allowed_user_list );
        }

        //redirect visiter to home page if he's not allowed to see this post or not login
        if( !is_home() && !is_user_logged_in() && !$is_user_allowed ){
            wp_redirect( home_url() ); // doesn't work
            exit();
            return $query;
        }

        return $query;
    }
}
new RavsPublic();
0

You'll usually want to die(); after a redirect. And, you can't put a redirect on EVERY page load, or it'll go into a redirect loop.

Try this:

add_action('pre_get_posts', 'wpse_109110');
function wpse_109110(){
    if (is_single()){
        wp_redirect(home_url());
        die();
    }
}

If you try to access a single post/page, it'll redirect to the home.

0

public is a reserved word in PHP. You will get an error until you rename that, at least I do.

Secondly, nothing is going to happen until you instantiate that class or call your redirect method statically.

class My_Public {
    public function __construct() {
        add_action('pre_get_posts', array($this, 'try_redirect'));
    }

    public function try_redirect() {
       wp_redirect( 'http://google.com' ); // doesn't work
       die;
    }
}
// This
// $mp = new My_Public();
// or this
add_action('pre_get_posts',array('My_Public','try_redirect'));

I would probably prefer template_redirect for this kind of redirect, rather than pre_get_posts. You have more information to work with for one, and you don't need the explicit die; with template_redirect, but the above does work.

There are other answers more directly addressing how to restrict users but I though I'd mention what I noticed and show you how to get your "test" code working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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