• Author archive pages are created for the no privilege ‘Contact” (or subscriber) role with a url of sitename/user_nicename. the_archive_title() returns author:(name of site admin), and the_archive_description() returns (director-title). The site admin (myself) created the contact page, so somehow wordpress thinks every subscriber is connected with the admin as author. Looking in the database, wp_capability is “contact” or “subscriber”, and wp_user_level is 0, so I stuffed the database OK. Somehow the archive page builder is creating a user_nicename url but returning the admin archive. But, interestingly, the loop isn’t posting any admin posts, the loop default archive query seems to know to not look for admin posts. The desired result is that no contact urls with user_nicenames ever appear, to protect the anonymity of my contacts.

    On my website you can see what’s happening at https://sacredbreeze.org/author/johnsmith-com/, a fake user I created.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter djmiller78

    (@djmiller78)

    Clumsy, but I added this function and it at least redirects non authors to welcome page. More elegant solutions welcome…

    add_action( 'template_redirect', function() {
    
      if ( is_author() ) {
    
        // get user id from display name
    
        $display_name=get_the_author();
    
        global $wpdb;
    
        $user = $wpdb->get_row( $wpdb->prepare(
    
          "SELECT ID FROM $wpdb->users WHERE display_name = %s", $display_name
    
        ) );
    
        $user_id = $user->ID;
    
        // get roles from user ide
    
        $user_meta=get_userdata($user_id);
    
        $user_roles=$user_meta->roles;
    
        $is_auth = in_array(('author'), $user_roles);
    
        $is_admin = in_array(('administrator'), $user_roles);
    
        $is_editor = in_array(('editor'), $user_roles);
    
        $is_contrib = in_array(('contributor'), $user_roles);
    
        $is_author = ($is_auth||$is_admin||$is_editor||$is_contrib);
    
        echo($is_author);
    
        if($is_author) {
    
          return;
    
        }
    
        else {
    
          wp_redirect( home_url(), 301);
    
          die;
    
        }
    
      }
    
    } );
    Moderator bcworkz

    (@bcworkz)

    If we request an author archive of a user who has no posts, we should normally get a Nothing Found page. This is regardless of user role. Requesting an author role user who has no published posts still should result in nothing found.

    If you’re getting an actual archive page for users who have no posts then some other code is altering the usual query.

    You mentioned “no privilege”. That has specific meaning in Ajax and admin-post.php requests, meaning the user is not currently logged in. It has nothing to do with user roles or capability. It’s probably not what you meant, but just in case it was, this is FYI.

    Using template_redirect to override whatever your site’s default behavior is, whether “normal” or not, is a reasonable approach. The alternative would be to alter the query through “pre_get_posts” to return the desired alternative page. I personally think getting nothing found is more informative than getting a welcome page for no apparent reason, but it’s your site, do as you wish.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Subscribers appearing in author archive’ is closed to new replies.