Support » Developing with WordPress » Show only if user-role is subscriber AND site visitor

  • Resolved anonymized-12966443

    (@anonymized-12966443)


    Hi, I am a bit stuck with the code below for my WordPress site. This code will be used in the Ad Inserter Head section to disable the Adsense Code for particular user-roles:

    <?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 because that doesn’t exist.

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not a fan of custom code,
    however to answer your question, a visitor does not have a Role.

    So instead, add a condition to check if a user is logged in or not:
    https://developer.ww.wp.xz.cn/reference/functions/is_user_logged_in/

    if ( is_user_logged_in() ) {
      $user = wp_get_current_user();
      $allowed_roles = array('subscriber', 'visitor');
      if ( array_intersect($allowed_roles, $user->roles ) || (!is_user_logged_in) ) 
        echo ...........
    • This reply was modified 2 years, 6 months ago by bcworkz. Reason: code format fixed
    Thread Starter anonymized-12966443

    (@anonymized-12966443)

    @corrinarusso, I solved the problem (credits goes to a stackoverflow user)

    <?php
    if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
    echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXX" crossorigin="anonymous"></script>'; 
    } ?>

    Just as a warning to you and others, there’s no capability in WordPress called ‘subscriber’, so that won’t check anything. At least not correctly.

    I’d suggest doing it this way. First, check if the visitor is logged in and then check if they have author capabilities. This is how I’d do it.

    if( is_user_logged_in() !== true || current_user_can ('edit_posts') !== true) {
        // Do your stuff here!
    }

    As my own note, I like to check for “not true” using “!== true” instead of “=== false” just as a little bit of additional (and probably over the top) security.

    • This reply was modified 2 years, 6 months ago by catacaustic.
    Thread Starter anonymized-12966443

    (@anonymized-12966443)

    @catacaustic thanks for your effort but the ads only need to appear for default user-role ‘subscriber’ and logged-out/site visitors. The code does exactly that and I don’t find any issues with it either.

    <?php
    if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
    echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXX" crossorigin="anonymous"></script>'; 
    } ?>

    The correct code is :
    $user = wp_get_current_user();
    $allowed_roles = array( ‘subscriber’);
    if ( array_intersect( $allowed_roles, $user->roles ) || !is_user_logged_in()) {
    // Your code here…
    }`

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show only if user-role is subscriber AND site visitor’ is closed to new replies.