Last active
April 3, 2024 17:11
-
-
Save MaximilianoRicoTabo/295c2dfc3c620d18115f8220d6098cc2 to your computer and use it in GitHub Desktop.
custom function to redirect member to their membership level's homepage when trying to access your site's front page (static page or posts page).
This file contains hidden or 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 | |
/** | |
* Function to redirect member to their membership level's homepage when | |
* trying to access your site's front page (static page or posts page). | |
* | |
* 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 custom_pmpromh_template_redirect_homepage() { | |
// Are we on the front page? | |
if ( is_front_page() ) { | |
// Get the hompage level for the current user. | |
$level_id = pmpromh_get_homepage_level_for_user(); | |
if ( ! empty( $level_id ) && pmpromh_allow_homepage_redirect( $level_id ) ) { | |
// Get the homepage for this level. | |
$member_homepage_id = pmpromh_getHomepageForLevel( $level_id ); | |
if ( ! empty( $member_homepage_id ) && ! is_page( $member_homepage_id ) && ! empty( get_post( $member_homepage_id ) ) ) { | |
// Redirect to the member homepage. | |
wp_redirect( get_permalink( $member_homepage_id ) ); | |
exit; | |
} | |
} | |
} | |
} | |
add_action( 'template_redirect', 'custom_pmpromh_template_redirect_homepage', 1, 1 ); | |
remove_action( 'template_redirect', 'pmpromh_template_redirect_homepage' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment