Ask your WordPress questions! Pay money and get answers fast! (more info)

Password protect entire wordpress site, apart from one page.

  • SOLVED

Hello,

This is a follow on to this question... [[LINK href="http://wpquestions.com/question/showLoggedIn/id/3794"]]http://wpquestions.com/question/showLoggedIn/id/3794[[/LINK]]

<strong>PROBLEM</strong>

I'm using Gravity Form User Registration add-on for a detailed data capture on profiles. But I am working on a wordpress site that is completely hidden from non members.

@Francisco suggested I use gravity forms for my detailed registration form, because all other advanced registration form plugins are failing me, due to profile custom fields.

So far the Gravity Form User Registration add-on seems to work great, with one problem... I can't add the GF registration form to my <strong>http://....../wp-login.php?action=register</strong> page.

How ever, Gravity Forms User Registration add-on has on option of redirecting from <strong>http://....../wp-login.php?action=register</strong> to any desired wordpress page

So I've created a page called <strong>User Registration</strong> and have placed the GF form on this page. I've then set it up so the <strong>http://....../wp-login.php?action=register</strong> link now redirects to the <strong>User Registration</strong> page.

But because I need my entire website to be only accessible by members, and hidden entirely from the public, I've been using the [[LINK href="http://wordpress.org/extend/plugins/password-protect-plugin-for-wordpress/"]]password protect plugin for wordpress[[/LINK]]. This works fine, but because my GF registration form now lives on a page within the site, when you click <strong>Register</strong> on the wp-login page, it redirects straight back to the wp-login page.


<strong>WHAT I'VE TRIED</strong>

I deactivated the [[LINK href="http://wordpress.org/extend/plugins/password-protect-plugin-for-wordpress/"]]password protect plugin for wordpress[[/LINK]], and installed the [[LINK href="http://wordpress.org/extend/plugins/member-access/"]]members access plugin[[/LINK]].

I configured these settings... [[LINK href="http://i.imgur.com/GnQcd.png"]]Plugin Settings[[/LINK]] and the [[LINK href="http://i.imgur.com/N74o4.png"]]Page Settings[[/LINK]].

But when you click on the <strong>Register</strong> button, it gets into a infinite loop for some reason, and eventually times out, and displays a blank page. Don't understand this?

Also, the [[LINK href="http://wordpress.org/extend/plugins/member-access/"]]members access plugin[[/LINK]] doesn't support custom post types.


<strong>WHAT I NEED</strong>

I need either...

<strong>( A )</strong> For my Gravity User Registration form to live on my <strong>http://....../wp-login.php?action=register</strong> like the normal registration form, but with my extra fields etc.

or

<strong>( B )</strong> A new password protection solution for my wordpress site that locks the entire site from non-members/public and redirects to the wp-login page, apart from my <strong>User Registration</strong> page, which in theory means my gravity form register redirection should work.


If you need anymore info then please let me know.

Thanks

Answers (3)

2012-01-26

Ivaylo Draganov answers:

Hello,

what you're trying to do is actually doable without a plugin. Try this for plan B:

/**
* Block site for non-logged in users
*
* @note pass to is_page() a detail about your login page - it can be the ID, path or title
* @var $login_registration_url holds the URL to the page where login/registration takes place
*/
add_action('get_header', 'wpq_member_only_site');

function wpq_member_only_site() {

// logged in users or visits to a specified page are allowed
if ( !is_user_logged_in() && !is_page('register') ) {

// the URL where login/registration takes place
$login_registration_url = get_home_url() . '/wp-login.php?action=register';

// redirect visitors
wp_redirect( $login_registration_url, 302);
exit;

}

}


Josh Cranwell comments:

Interesting.

So by adding this into my functions.php, how do I define the page/pages I want to be visible to public?

example my 'User Registration' page.

Thanks


Josh Cranwell comments:

I've just seen where I change, gimme 2 secs


Ivaylo Draganov comments:

<blockquote>So by adding this into my functions.php, how do I define the page/pages I want to be visible to public?</blockquote>

You can add any condition to the <em>if</em> statement:

if ( !is_user_logged_in() && !is_page('register') && !is_page(23) && !is_category('public_posts') ) {


Ivaylo Draganov comments:

Multiple pages can be passed as an array:

is_page(array('register', 'public_page', 23, 'Page Title'))


Josh Cranwell comments:

It seems to work, but in the wrong way.

Please see below...

<strong>
http://example.com/
http://example.com/about
http://example.com/downloads
http://example.com/page
http://example.com/help
http://example.com/news
...etc
</strong>

...these all need to redirect to wp-login if your not a member.


The register button ( http://example.com/wp-login.php?action=register) on the wp-login page needs to redirect to...

<strong>http://example.com/user-registration</strong>


Does this make sense?

Thanks Ivaylo


Josh Cranwell comments:

<strong>http://example.com/
http://example.com/about
http://example.com/downloads
http://example.com/page
http://example.com/help
http://example.com/news
...etc</strong>

This represents my entire site.


Josh Cranwell comments:

<blockquote>...these all need to redirect to wp-login if your not a member.</blockquote>

I meant to say...

<strong>these all need to redirect to wp-login if your not logged in.</strong>


Ivaylo Draganov comments:

Then the conditional line should be:

if ( !is_user_logged_in() && !is_page('user-registration') ) {


This means that not logged in users will be redirected, except if the page being requested is 'user-registration'.

And what do you mean by "wrong way"?


Josh Cranwell comments:

For example, if I got to...

<strong>http://example.com/</strong>

it redirects to <strong>http://example.com/user-registration</strong>


I need it to do this...

When you visit <strong>http://example.com/</strong> or any other part of the site, it has to redirect you to the wp-login page. If you successfully log-in, it has to redirect you back to <strong>http://example.com/</strong> or what ever URL you we're originally trying to visit.

But for the exception of the <strong>http://example.com/user-registration</strong> page. This is the only page I want visible to the public, and not to have any redirect.


Thanks


Ivaylo Draganov comments:

Okay, this should get you there:

/**
* Block site for non-logged in users
*/
add_action('get_header', 'wpq_member_only_site');

function wpq_member_only_site() {

// logged in users or visits to a specified page are allowed
if ( !is_user_logged_in() && !is_page('user-registration') ) {

// vars
$redirect_after_login = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'];
$login_url = get_home_url() . '/wp-login.php?redirect_to=' . $redirect_after_login;

// redirect visitors
wp_redirect( $login_url, 302);
exit;

}

}


Josh Cranwell comments:

Thanks it seems to nearlly work but it's a little buggy.

Because I'm using WPML, its adds this...

?lang=es

and

?lang=it

for different languages. If I click my log-out button on the site when I'm in a different language it's gets into a weird infinite loop...

Is there anyway of stopping this? Thanks

Josh


Josh Cranwell comments:

I was not meant to post the URL to my site then...

I was meant to post this...

http://www.example.com/wp?lang=eswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp-loginphpredirecttohttpwwwexamplecomwplangeswp


Josh Cranwell comments:

Sorry I didn't explain myself very well...

Once logged in, if I switch to spanish, it adds this...

http://www.example.com/?lang=es

and for italian it adds this...

http://www.example.com/?lang=it

On my site, I have a log out button using this code... <?php echo wp_logout_url( home_url() ); ?>

If I'm on another language (not the default english), if I click that log out button, it creates the infinite loop I posted above.


Ivaylo Draganov comments:

Give me some time to make at test case with WPML.


Josh Cranwell comments:

Thank you, let me know if you need WPML


Ivaylo Draganov comments:

Oh, sorry WPML is no longer available for free download... Hm, how shall we proceed on this...


Josh Cranwell comments:

For debugging purposes... http://wtrns.fr/plC7PpOp82zHCA

To add the language selector on your site, add this...

<?php do_action('icl_language_selector'); ?>


Ivaylo Draganov comments:

I think I've nailed it :--) WPML is filtering the get_home_url() function and appending the "lang" query string (e.g. ?lang=es). And since I was using that function it got into and endless loop because it was passing the same query string... Well - the redirect URL was simply messed up. Here's the code that works:

/**
* Block site for non-logged in users
*/
add_action('get_header', 'wpq_member_only_site');

function wpq_member_only_site() {

// logged in users or visits to a specified page are allowed
if ( !is_user_logged_in() && !is_page( 'user-registration' ) ) {

// vars
$redirect_after_login = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$login_url = wp_login_url( $redirect_after_login );

// redirect visitors
wp_redirect( $login_url, 302 );
exit;

}

}


I made some tests and it seems there are no issues. Let me know if it's OK.


Josh Cranwell comments:

Dude, you are a legend!!!

I've tested it through out my site. Seems to work perfect. Even when on different languages and no problems logging out.

I think you nailed it. Cant thank you enough!!!

If I notice anything weird I'll drop you an email but think we're pretty good.

Thanks Ivaylo

2012-01-26

Julio Potier answers:

Hard, but understandable :

1) you redirect the login page on a wordpress page
2) you got a plugin which redirect non logged users on login page
3) go to (1)
...
i'll think about a solution ...


Josh Cranwell comments:

1) I'm redirecting the 'register' button on the login page to a wordpress page
2) I've got a plugin which redirects non logged users on login page
3) go to (1)

...it's tricky

2012-01-26

Ross Wilson answers:

Odd, you should be able to use the member access plugin. Do you have a link for your registration page you can share. My guess is that the page you are submitting the registration to is causing the redirect.

Ross