0

I added the possibility to register on my site (with the role of subscriber), however each registration creates an author page for the user, but I do not want these pages to be indexed, only the pages of authors with roles above.

Something like the code below, but theres no get_user_role function, so i do not know how to get to that result. I would be grateful if anyone could help.

<meta name="robots" content="<?php if( is_page('author') ) && get_user_role('subscriber'); {  
    echo "noindex, nofollow";
  }else{
    echo "index, follow";
  } ?>" />

Edit:

This is the code of @belinos answer. I put it inside header.php, the problem is that an error appears on the online site, on localhost it does not appear.

<?php $curauth = ( isset( $_GET[ 'author_name' ] ) ) ? get_user_by( 'slug', $author_name ) : get_userdata( intval ($author ) ); 

$auth_data = get_userdata( $curauth->ID ); 

if ( in_array( 'subscriber', $auth_data->roles )) { ?>
    <meta name="robots" content="noindex, nofollow"/> 
<?php } else { ?>
    <meta name="robots" content="index, follow"/>
<?php } ?>

This is the error:

Warning: in_array() expects parameter 2 to be array, null given in /home/u836053643/public_html/wp-content/themes/gamersaction/header.php on line 35

And that is the line 35:

if ( in_array( 'subscriber', $auth_data->roles )) { ?>

The code works perfectly but is showing this error.

1

The function you want is get_userdata(). Since you need to do this outside the loop, the process is a little less straight-forward.

The first thing you need to do is set up a variable called $curauth which is an object that you create by accessing the database by using the $_GET[] superglobal.

$curauth = ( isset( $_GET[ 'author_name' ] ) ) ? get_user_by( 'slug', $author_name ) : get_userdata( intval ($author ) );

This assigning of $curauth must be in your author.php file.

After that, we can then use the get_userdata() function and feed it the ID from $curauth.

$auth_data = get_userdata( $curauth->ID );

And from there your conditional becomes:

if ( in_array( 'subscriber', $auth_data->roles ) ) {
    // No Follow Code
} else {
    // Follow Code
}

My advice would be to make this all a function in your functions.php file:

function author_nofollow( $author ) {

    $auth_id = $author->ID;
    $auth_data = get_userdata( $auth_id );

    if ( in_array( 'subscriber', $auth_data->roles ) ) {
        echo 'noindex, nofollow';
    } else {
        echo 'index, follow';
    }
}

Then you would just call it like this:

<meta name="robots" content="<?php author_nofollow( $curauth ); ?>">
  • This is appearing on the online site, in localhost does not appear. Do you know what it can be? Warning: in_array() expects parameter 2 to be array, null given in /home/u836053643/public_html/wp-content/themes/gamersaction/header.php on line 35. You can see this here gamersaction.pe.hu . Line 35 is this if ( in_array( 'subscriber', $auth_data->roles )) { – Gabriel Souza Mar 27 '17 at 21:36
  • You're going to have to edit your original post with the code from that page since I cannot see the PHP in context. Or provide a link to pastebin or something. It looks like $auth_data or $curauth is not being defined properly. – Cedon Mar 27 '17 at 22:48
  • Edited, please take a look – Gabriel Souza Mar 27 '17 at 23:16
  • $curauth needs to be defined in author.php. – Cedon Mar 28 '17 at 1:30
  • I already had it set there, now I removed it from the header and kept only in the author.php and now the subscriber is indexing. What am I doing wrong? – Gabriel Souza Mar 28 '17 at 1:42
0

The easiest way to accomplish this is to conditionally call wp_no_robots() (or echo your custom <meta> element) in a wp_head action hook. This has the added benefit of being modular - you could throw it in a plugin instead of performing theme modifications, if desired.

In the instance of an author archive, get_queried_object() will (usually - setting other query vars can throw this off) return an instance of WP_User - the same type of object that get_userdata() returns.

function wpse261293_noindex_subscriber_profiles() {
  // Ignore non-author-archive content
  if( !is_author() )
    return;

  // Get a WP_User object for the author
  $author = get_queried_object();

  // If 'subscriber' is the author's only role, print a nofollow robots meta element
  if( count( $author->roles ) === 1 && in_array( 'subscriber', $author->roles ) )
    wp_no_robots(); // Alternately, replace this with echo( 'your_custom_meta_element' )
}

add_action( 'wp_head', 'wpse261293_noindex_subscriber_profiles' );

If you or another plugin have custom user roles which could be granted to subscribers, you may need to alter the logic to account for them - potentially by checking if the author simply does not have administrator, editor, contributor, etc. roles.

  • The function is echoing in all author pages, not just in the subscribers – Gabriel Souza Mar 28 '17 at 15:08
  • Well that's weird - did you remove the meta-element you were working on from header.php before-hand? You could try replacing wp_no_robots() with echo( '<!-- wpse261293 TEST -->' ); to make sure that the robots tag you're seeing is actually coming from the hook in my answer. – bosco Mar 28 '17 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.