Jump to content

Show only if user-role is subscriber AND site visitor


heathcliff
 Share

Recommended Posts

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 by heathcliff
Link to comment
Share on other sites

  • heathcliff changed the title to Show only if user-role is subscriber AND site visitor

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.

  • Like 1
Link to comment
Share on other sites

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!

🙏

Link to comment
Share on other sites

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 } }

 

Link to comment
Share on other sites

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 &check;<br>";
    }
    else {
        echo "$uid &times;<br>";
    }
}

image.png.68cf5503fd07c8d991f3176d5e2832ef.png

Link to comment
Share on other sites

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 
	}

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.