Last active
March 14, 2024 04:47
-
-
Save adeel-raza/72a268ff9e3572048308be34146e26ab to your computer and use it in GitHub Desktop.
Personalize content for specific WordPress or LearnDash users using WordPress 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
// Show personalized content to students | |
add_shortcode( 'sv_personalize', 'nt_student_specific_content' ); | |
function nt_student_specific_content( $atts , $content = null ) { | |
if( !isset( $atts['values'] ) ) { | |
return $content; | |
} | |
$atts['values'] = preg_replace('/\s*,\s*/', ',', $atts['values']); | |
$users = explode( ",", $atts['values'] ); | |
$current_user = wp_get_current_user(); | |
if($atts['field'] == "email") { | |
foreach($users as $user) { | |
if($atts['partial']) { | |
if(strstr($current_user->user_email, $user) !== false) { | |
return apply_filters( "sv_personalize", $content ); | |
} | |
} else { | |
if($current_user->user_email == $user) { | |
return apply_filters( "sv_personalize", $content ); | |
} | |
} | |
} | |
return false; | |
} else { | |
if( in_array( $current_user->user_login, $users ) ) { | |
return apply_filters( "sv_personalize", $content ); | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the above code in the end of your theme's
function.php
file and then this shortcode anywhere on your site.If you want to show content based on user email then add comma separated values in match entry and field as
email
.You can also add set a partial match
(true/false)
parameter this will perform partial matches on email liketest3
this will show the content to any user with havingtest3
in their emailsOption 1 match by email
Option 2 match by username