Created
May 29, 2018 13:45
-
-
Save actual-saurabh/adb4bb90eef43e516bf259166f4f5143 to your computer and use it in GitHub Desktop.
Redirect Logged in Users on pages with [lifterlms_login] shortcode
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 // Do not copy this line | |
// Copy from under this line and paste into your child theme's functions.php | |
add_action( 'template_redirect', 'llms_redirect_logged_in_on_login_shortcode' ); | |
function llms_redirect_logged_in_on_login_shortcode(){ | |
// don't do anything if a user is not logged in | |
if( ! is_user_logged_in() ){ | |
return; | |
} | |
// don't do anything on non-singular screens | |
if( ! is_singular() ){ | |
return; | |
} | |
global $post; | |
// don't do anything on a post with no login shortcode | |
if( ! has_shortcode( $post->post_content, 'lifterlms_login') ){ | |
return; | |
} | |
/* | |
* if we've reached here, this is a logged in user | |
* on a singular post type that contains the shortcode lifterlms_login | |
*/ | |
// get the loggedin_redirect attribute | |
preg_match('/\[lifterlms_login.* loggedin_redirect=\"(.*)\".*\]/iU', $post->post_content, $result); | |
// don't do anything if the attribute is not found | |
if( empty( $result ) || ! is_array( $result ) ){ | |
return; | |
} | |
// if we've reached here, the loggedin_redirect parameter is set | |
// don't do anything if the url is not valid | |
if ( filter_var( $url, FILTER_VALIDATE_URL ) === false ) { | |
return; | |
} | |
// everything cool, let's redirect! | |
wp_safe_redirect( $url ); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment