Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Last active September 12, 2024 14:16
Show Gist options
  • Save kimwhite/f495eb82d793de19c17ee5d9079dee79 to your computer and use it in GitHub Desktop.
Save kimwhite/f495eb82d793de19c17ee5d9079dee79 to your computer and use it in GitHub Desktop.
<?php // do not copy this line.
/**
* This recipe will redirect non-members from your blog page if all of the post are protected and you have hidden from non-memberts.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_redirect_away_if_blog_is_empty() {
global $wp_query;
global $wp;
//Is this the blog page?
if ( $wp_query->query_vars['pagename'] === 'blog' ) {
//Theres no posts in the blog right now so redirect away
if ( ! have_posts() ){
wp_redirect( '/activate-membership/');
die();
}
}
}
add_action( 'template_redirect', 'mypmpro_redirect_away_if_blog_is_empty' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment