Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active March 14, 2024 04:47
Show Gist options
  • Save adeel-raza/72a268ff9e3572048308be34146e26ab to your computer and use it in GitHub Desktop.
Save adeel-raza/72a268ff9e3572048308be34146e26ab to your computer and use it in GitHub Desktop.
Personalize content for specific WordPress or LearnDash users using WordPress Shortcode
// 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;
}
}
}
@adeel-raza
Copy link
Author

adeel-raza commented Jun 25, 2019

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 like test3 this will show the content to any user with having test3 in their emails

Option 1 match by email

[sv_personalize match="[email protected], [email protected], test3" field="email" partial="true"]
This content will be displayed specifically to users with username 1 and username 2
[/sv_personalize]

Option 2 match by username

[sv_personalize values="username1, username2" field="username"]
This content will be displayed specifically to users with username 1 and username 2
[/sv_personalize]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment