Block search engines but allow users only

Is there any way to able to block search engines in the privacy settings but still block non logged in users?

Is this option just a code I can add to something like Restricted Site Access Plugin?

  • ThePath

    Hi, let me get this right:

    You want to block search engines and block users that are not logged in. Is that right?

    But if say a user logs out they wont get back in if you have your way? Or if they delete their cache/cookies they will no longer have access to the site. Basically my point is that nobody will stay logged in forever.

    If you want to block non-logged in users then put this at the beginning of each page/post template file:

    <?php if ( ! is_user_logged_in() ){ ?>

    Obviously you have to close it too with <?php } ?>

    A plugin may do the job too but I have never had cause to look for this kind of plugin. But you cant do it from any standard WP setting

  • Jonathan

    For Search Engines:
    Wordpress privacy setting (adds no-follow no-index to header) and For those bots that actually follow direction Maybe a robots.txt file
    with something like this in it... google robots.txt wordpress

    User-agent: *
    Disallow: /wp-
    Disallow: /feed/
    Disallow: /trackback/
    Disallow: /rss/
    Disallow: /comments/feed/
    Disallow: /page/
    Disallow: /date/
    Disallow: /comments/
    Disallow: /cgi-bin/
    Disallow: /2009/
    Disallow: /2010/
    Disallow: /*?*
    Disallow: /iframes/
    Disallow: /recommends/

    For Users:
    ThePath is 100% correct. I would also use the is_user_logged_in and direct the user to the login page.

    So if the user isn't logged in, they are directed to the login page. Logged in users can access everything.

    Guess you want the code, Ummm, Okay add this to your functions.php file in your theme.

    /* Redirects non logged in users to the login page. */
    add_action( 'template_redirect', 'you_need_to_log_in', 1 );
    /**
     * Redirects users that are not logged in to the 'wp-login.php' page.
     */
    function you_need_to_log_in() {
    
    		/* If using BuddyPress and on the register page, don't do anything. */
    		if ( function_exists( 'bp_is_current_component' ) && bp_is_current_component( 'register' ) )
    			return;
    
    		/* Else, if the user is not logged in, redirect to the login page. */
    		elseif ( !is_user_logged_in() )
    			auth_redirect();
    }

    Jonathan

  • ThePath

    Nice code dude!

    If you are going into your page templates to utilise the is_user_logged_in function then you could do it this way to:

    <?php if ( ! is_user_logged_in() ){ ?>
            <h2>Please Login</h2><ul>
            	<div style="float:left; width:100%;">
                <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
               	<p><input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" /><label for="log"> Username</label></p>
                <p><input type="password" name="pwd" id="pwd" size="20" /><label for="pwd"> Password</label></p>
                <p><input type="submit" name="submit" value="Send" class="button" /></p>
                <p><label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label><input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /></p>
                </form>
            	</div>
                <div class="cleared"></div>
                <li><a href="<?php echo get_option('home'); ?>/wp-register.php">Register</a></li>
                <li><a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a></li>
                </ul>
    			<?php } else { ?>
    Redirect to page you want
    		                <?php }?>
                <?php }?>

    OR even simpler just:

    <?php if ( ! is_user_logged_in() ){
    wp_login_form();
     } else { ?>
    Redirect to page you want
    <?php }?>

    Jonathons solution looks more elegant :wink: you gotta respect the usage of the functions.php! (sigh, *wishes he was a better coder :slight_frown:* )

  • Jonathan

    @The Path,

    *wishes he was a better coder :slight_frown:* )

    Your code is perfect. It is what I would use to lockout non logged in members :wink:

    I was just trying to make it

    "a code I can add to something like Restricted Site Access Plugin"

    so was trying for something plugin like - lol
    But I would go your route as it offers more variety and customization :wink:

    And I'm going to have to checkout that plugin @masonjames :wink:

  • ThePath

    @Jonathon thanks yeh Im getting better and learning as I go but definitely more of a bodger than a coder. To be fair its probably guys like yourself that I get these solutions from in the first instance and I just got lucky and remembered it! :stuck_out_tongue:

    WP has so much to learn about it and Im still a noob when it comes to making plugins and the old functions.php file but I will no doubt dig deeper into it. I have actually released a plugin if you can believe that! lol. Its a fairly basic one actually and needs other plugins to make it useful: ThePath Tabbed Widget.

    I got it woking just as code on one of my sites and I kept using it on others and thought, hey would be easier if this was a plugin I could install quicker! It would be cool to expand it and I have loads ideas to make it better, just lacking in the coding skills at the moment.

  • Jonathan

    @The Path,

    No worries, we all are in various stages of ability. I've just been playing a bit longer, but I am in no way in the same league as the folks that hang around here - the WPMU DEV Support Dudes and Developers. They teach me something new everyday.

    Oh, and as a recommendation - try to learn php outside of wordpress. Wordpress really makes things complicated - not being allowed to touch core files, and filtering / hooking into everything drives me nuts.

    Here are some great starter videos...
    http://phpacademy.org/videos/index.php?all

    Oh, you are well ahead of me - I don't even have a plugin listed :slight_frown: Guess that happens when I do lots of custom stuff, not really for public consumption - lol. But seriously, great job on the tabbed widget.

    Jonathan

  • Mason

    Actually the main draw for me to WordPress as opposed to other platforms is because of the knowledge sharing that takes place here.

    I've never released a plugin of my own (though I've been a project manager and created the specs for a few that are currently up) and my PHP is poor at best.

    We learn from you all as well (Jonathan, I think more than one of your posts I have marked in my 'favorites' for reference). I've found that debugging is a great teacher :slight_smile: and part of what makes the work here a lot of fun.

    Thanks to everyone for continuing to share around here!

  • Jonathan

    We learn from you all as well (Jonathan, I think more than one of your posts I have marked in my 'favorites' for reference

    Wow, really - thanks :slight_smile: Now I feel special :wink:

    And Yeah, I agree, the wordpress community really does know how to be open about information - and as such definitely makes it the ideal entry point. Just within the last year... the resources for code has multiplied itself over and over and over. I remember 3 years ago googling myself to death trying to figure something out. 6 years ago... there wasn't much. But, Today I get to be picky about who I'm going to listen too. Unreal.

    Thanks @masonjames for allowing us to share - and let us know if we step on your toes :wink:

  • stacy

    Oh okay, so "Only allow logged in users to see all blogs" is also "block all search engines including bp activity". duh. ( I'm doing this really to block particular blogs from activity)

    @Masonjames, oops I was already using this to "block search engines but allow normal visitors " for users blogs but didn't check out the site admin override option to block non logged in users and bp activity feed for just a few admin blogs. So it fixes a few needs since I wanted 1) block users blog's from google, yahoo, ect. 2) Have only logged in users see admin blogs and stop these from flooding the activity stream because this code for blocking a particular user from activity wasn't working.

    I will try Network wide aggregation blog plugin to still show posts from select users blogs or just live without it.

    @Thepath. Thanks so much for the reasoning.

    But if say a user logs out they wont get back in if you have your way?

    There's always the login page available with block options like these, the users just have to know it or be redirected to it.

    @Jonathan. I totally agree with your last comment. I even think if there was a backend widget for posting fixes stumbled upon then there would be more giving back for wordpress in the form of answering questions.

  • ThePath

    @stacey Sounds like you are running a tight ship. I spose if the login form is always there then it will be OK but could get slightly tedious.

    Yeh WP is the best, very open and sharing community although more and more paid sites like this one are springing up, which isnt a bad thing I spose. I do worry slightly that it will become too commercial in the future and people will abandon the free forums and the WP repository to make some readies on ecommerce sites. Dont get me wrong I make a decent bit of money of WP and am more than willing to pay and support my WP betters :wink: Anyway shutup Neil youve made this thread offtopic. Sorry :stuck_out_tongue:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.