heathcliff Posted September 11, 2021 Share Posted September 11, 2021 (edited) Hi, I am a bit stuck with the code below for my Wordpress site: <?php $user = wp_get_current_user(); $allowed_roles = array('subscriber', 'visitor'); if ( array_intersect($allowed_roles, $user->roles ) ) echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXX" crossorigin="anonymous"></script>' ?> I want the Adsense code only be shown in the HEAD section for site visitors and the user role SUBSCRIBER. I tried 'visitor' but that didn't worked. Thanks in advance. Edited September 11, 2021 by heathcliff Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/ Share on other sites More sharing options...
requinix Posted September 11, 2021 Share Posted September 11, 2021 array_intersect will tell you the items that were present in both arrays. All you're doing now is ensuring that the array it returns is not empty - it has at least one of the two roles. If you want to require both roles then you can (a) use array_diff or (b) continue with array_intersect but count the number of shared roles it found. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589849 Share on other sites More sharing options...
heathcliff Posted September 12, 2021 Author Share Posted September 12, 2021 13 hours ago, requinix said: array_intersect will tell you the items that were present in both arrays. All you're doing now is ensuring that the array it returns is not empty - it has at least one of the two roles. If you want to require both roles then you can (a) use array_diff or (b) continue with array_intersect but count the number of shared roles it found. Dear @requinixfirst of all thank you sincerely for taking your time helping a stranger out I sincerely appreciate it. About your answer, do you have an online example because I am not a hero in PHP. Thanks in advance! 🙏 Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589864 Share on other sites More sharing options...
heathcliff Posted September 12, 2021 Author Share Posted September 12, 2021 I found this code on the internet. Maybe this can help me? $current_user = new WP_User(wp_get_current_user()->id); $user_roles = $current_user->roles; foreach ($user_roles as $role) { if ($role == 'subscriber' ){ //code here for subscribers } if ($role == 'editor' ){ //code here for editors } } Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589865 Share on other sites More sharing options...
Barand Posted September 12, 2021 Share Posted September 12, 2021 This may be closer $allowed_roles = ['subscriber', 'visitor']; $user_roles = [ 'A' => ['subscriber', 'editor'], 'B' => ['subscriber', 'other', 'visitor'], 'C' => ['manager', 'visitor'], 'D' => ['manager', 'editor' ] ]; foreach ($user_roles as $uid => $roles) { if ( count( array_intersect($roles, $allowed_roles)) == count($allowed_roles) ) { echo "$uid ✓<br>"; } else { echo "$uid ×<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589866 Share on other sites More sharing options...
jarvis Posted September 13, 2021 Share Posted September 13, 2021 Is visitor a custom role? As a Wordpress user is generally only assigned one role, can you not simply do something like: #only show content to specific roles $user = wp_get_current_user(); if ( $user == 'subscriber' || $user == 'visitor' ){ // adsense code } 1 Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589878 Share on other sites More sharing options...
Barand Posted September 13, 2021 Share Posted September 13, 2021 46 minutes ago, jarvis said: Is visitor a custom role? I haven't a clue. It's just test data. Could just as well be [ 'X', 'Y', 'Z' ] Quote Link to comment https://forums.phpfreaks.com/topic/313725-show-only-if-user-role-is-subscriber-and-site-visitor/#findComment-1589879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.