Sure. Here’s an example of an mu-plugin
which assumes that you have set all your regular posts to be protected, and then overrides that protection if your site detects that it has a bot “looking” at it:
<?php
function kts_update_page_options( $options ) {
if ( isBot() ) {
if ( is_singular( 'post' ) ) { // unprotect all regular posts from bots
return $options;
}
unset( $options['level0_posts'] ); // or
// unset( $options['level1_posts'] ); //choose which you need
}
return $options;
}
function kts_options_init() {
add_filter( 'option_ws_plugin__s2member_options', 'kts_update_page_options' );
}
add_action( 'parse_query', 'kts_options_init' );
# http://jaspreetchahal.org/php-check-if-search-engine-bot/
function isBot() {
return (
isset( $_SERVER['HTTP_USER_AGENT'] ) // check if the user agent header key exists
&& preg_match('/bot|crawl|spider|mediapartners|slurp|patrol/i', $_SERVER['HTTP_USER_AGENT'] )
);
}
Obviously, you will need to modify this if you want just to show excerpts or achieve some other purpose.
As I say, this is not secure or guaranteed to be accurate. But if you find that the isBot
function doesn’t do quite what you want, then it’s easy to Google for an alternative function that will enable PHP to detect bots or a specific bot.