Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Hide Author page from others

If I understood correctly, redirecting based on post count, as you are doing, is wrong. What you need is to check if current user is the same that the author’s profile; if it is the same, do nothing, it is not the same, redirect to own profile; if it a guest user, redirect to login page:

add_action( 'template_redirect', 'cyb_template_redirect' );
function cyb_template_redirect() {

    // Check if we are on author template
    if ( is_author() ) {

        // Check if current user is logged in
        if( is_user_logged_in() ) {

             // User is logged in

             // Get the id of the user being displayed
             $viewing_profile_id = get_query_var( 'author' );

             // Get the id of current user
             $current_user_id = get_current_user_id();

            // if current user and profile being displayed is not the same,
            // then redirect to current user author page
            if ( $viewing_profile_id != $current_user_id ) {

                wp_redirect( get_author_posts_url( $current_user_id ) );
                exit;

            }

        } else {

            // User is not logged in, redirect to login with redirect parameter
            // set to current user profile url, so the user will be redirected to
            // own profile if login is successful
            wp_redirect( wp_login_url( get_author_posts_url( $current_user_id ) ) );
            exit;

        }

    }
}

NOTE: code not tested, just written here

Related Posts:

  1. Action wp_login_failed not working if only one field is filled out
  2. Restricting frontend acess based on user role otherwise redirect to login form
  3. wp_get_referer not working properly after wp_redirect
  4. Login redirects and query strings
  5. How can I login as admin after redirect to custom login page
  6. Redirect wp-login
  7. Do more action after login successfully
  8. Require re-login when logged-in user attempts to access restricted page
  9. This webpage has a redirect loop issue
  10. WordPress site login Redirect
  11. Problem in auto login after registration
  12. WordPress Redirect After logging
  13. redirect_to not Including Hashtag from URL
  14. A way to redirect users when they log in based on their current page
  15. Login form- no feedback
  16. In Django, how do I know the currently logged-in user?
  17. Can I programmatically login a user without a password?
  18. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  19. Is there any way to rename or hide wp-login.php?
  20. How to login with email only no username?
  21. How can I redirect user after entering wrong password?
  22. Increase of failed login attempts, brute force attacks? [closed]
  23. Login page ERROR: Cookies are blocked due to unexpected output
  24. Separate registration and login for different roles
  25. SSO / authentication integration with external ‘directory service’
  26. Preventing session timeout
  27. How reduce wordpress login session timeout time?
  28. How to prefill WordPress registration with social details
  29. Check for correct username on custom login form
  30. Disallow user from editing their own profile information
  31. I can’t access my site via wp-admin
  32. ‘Password field is empty’ error when using autofill in Chrome
  33. Removing username from the ‘wordpress_logged_in’ cookie
  34. How to show ‘login error’ and ‘lost password’ on my template page?
  35. What is $interim_login?
  36. Custom login form
  37. How to prefill the username/password fields on the login page
  38. wp_signon returns user, but the user is not logged in
  39. Adding extra authentication field in login page
  40. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  41. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  42. wp_signon() does not authenticate user guidance needed
  43. What exactly is ReAuth?
  44. What are the differences between wp_users and wp_usermeta tables?
  45. Login members using web services
  46. Make my wordpress blog remember my login “forever”
  47. How to check in timber if user is loggedin?
  48. How do I change the language of only the login page?
  49. Disable WordPress 3.6 idle logout / login modal window / session expiration
  50. Stop WordPress from logging me out (need to keep me logged in)
  51. Woocommerce registration page [closed]
  52. How to disable autocomplete on the wp-login.php page
  53. Share login data/cookies between multiple installations
  54. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  55. How to pass users back and forth using session data?
  56. How do I change the logo on the login page?
  57. Why does WordPress hide the reset password key from the URL?
  58. Is it possible to sign in with user_email in WordPress?
  59. How to use current_user_can()?
  60. Avoid to load default WP styles in login screen
  61. WordPress registration message
  62. How to fake a WordPress login?
  63. how to display the wordpress login and register forms on a page?
  64. Does wp_logout_url() destroy a session? (Logging out question)
  65. How can I send a welcome email to a user AFTER they login for the first time?
  66. Can not login with correct username and password
  67. Website Visible only to Registered users
  68. How can i increase the login expiration length?
  69. How do I use add_action from a class method?
  70. How to remove the WordPress logo from login and register page?
  71. How can I add a custom script to footer of login page?
  72. Brute force attack?
  73. Customize wp_new_user_notification_email()
  74. Need to execute a cron job
  75. Password change when the user login first time
  76. Login email after registration never sent or received
  77. How can I create a separate blog that is private?
  78. How to keep always logged in development environment
  79. Add Confirm Password field in wp-login.php Password Reset page
  80. Integrate recaptcha and wp_signon – what is needed?
  81. Stop users from logging in from multiple locations
  82. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  83. custom login page redirect to logged in user profile page
  84. Email address or username used to login in wordpress
  85. How do I check if a post is private?
  86. Front-end login: Redirect user to the post they had created
  87. Receiving “This content cannot be displayed in a frame” error on login page
  88. My login form does not work
  89. Programmatically log in a wordpress user
  90. Getting “Cookies are blocked or not supported by your browser” on login page
  91. What is the purpose of logging out after WordPress upgrade?
  92. Is it alright for two people to simultaneously be logged into a WP site as administrator?
  93. wp-login.php redirecting to HTTPS
  94. Display last login time
  95. How to customise wp-login.php only for users who are setting a password for the first time?
  96. Intentionally Force Failed Login WordPress
  97. How do I turn off the ability to login?
  98. Gaining Login Access via the Database
  99. How can I test the login for an expired session?
  100. Give visitor access to password protected page/post via external script
Categories login Tags author-template, login, wp-redirect
Need alternative get_posts function for custom post type
Can someone tell me where I’ve gone wrong with this plugin’s Settings page?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress