How I can limit access to anspress pages?

2.83K viewsIssues
1

I’m trying to limit access to certain “pages” created by anspress to users logged in. That is, I would like the public to have access to view only the individual questions (I’m showing only the excerpt), but not the other pages. Is it posible?

This is what I’m trying:

function login_required() {
    global $post;
    if( is_page( array( 'community','user' ) ) && ! is_user_logged_in() ) {
        wp_redirect( home_url() ); 
        exit();
    }
}
add_action( 'template_redirect', 'login_required' );

 

 

[edited]

I managed to do it, but in an alternative way. As anspress pages are not “real”, I override user.php and base.php including this code:

<?php if (ap_user_can_ask()): ?>
....

<?php endif; ?>
 <?php ap_get_template_part('login-signup'); ?>

That is actually a clever thing to do: show users “here is this question, and it have answers, please login if you want to see this”.

It could be further improved to “you must have this much Reputation to view this answer”.

If not in core, it should be implemented in a plugin someday. Please keep us(me) posted, Chema!