Created
July 14, 2021 18:46
-
-
Save ipokkel/deee4ac533ff9850a0d5de9e58fce0eb to your computer and use it in GitHub Desktop.
Show a login/register link at the bottom of any post that uses the membership shortcode or the Require Membership Block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Show a login/register link at the bottom of any post that uses the membership shortcode | |
* or the Require Membership Block. | |
* | |
* Note this doesn't check for specific membership levels. | |
* | |
* 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 show_login_register_links_on_member_pages( $content ) { | |
//bail if PMPro isn't loaded | |
if ( ! function_exists( 'pmpro_has_membership_access' ) ) { | |
return $content; | |
} | |
//does this post use the membership shortcode? | |
if ( ( has_shortcode( $content, 'membership' ) || has_block( 'pmpro/membership', $content ) ) && ! pmpro_hasMembershipLevel() && pmpro_has_membership_access() ) { | |
$pmpro_content_message_pre = '<div class="pmpro_content_message">'; | |
$pmpro_content_message_post = '</div>'; | |
$sr_search = array( '!!levels!!', '!!referrer!!', '!!login_url!!', '!!levels_page_url!!' ); | |
$sr_replace = array( '', rawurlencode( site_url( $_SERVER['REQUEST_URI'] ) ), wp_login_url(), pmpro_url( 'levels' ) ); | |
//get the correct message to show at the bottom | |
if ( is_feed() ) { | |
$newcontent = apply_filters( 'pmpro_rss_text_filter', stripslashes( pmpro_getOption( 'rsstext' ) ) ); | |
$content .= $pmpro_content_message_pre . str_replace( $sr_search, $sr_replace, $newcontent ) . $pmpro_content_message_post; | |
} elseif ( is_user_logged_in() ) { | |
//not a member | |
$newcontent = apply_filters( 'pmpro_non_member_text_filter', stripslashes( pmpro_getOption( 'nonmembertext' ) ) ); | |
$content .= $pmpro_content_message_pre . str_replace( $sr_search, $sr_replace, $newcontent ) . $pmpro_content_message_post; | |
} else { | |
//not logged in! | |
$newcontent = apply_filters( 'pmpro_not_logged_in_text_filter', stripslashes( pmpro_getOption( 'notloggedintext' ) ) ); | |
$content .= $pmpro_content_message_pre . str_replace( $sr_search, $sr_replace, $newcontent ) . $pmpro_content_message_post; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'show_login_register_links_on_member_pages', 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment