I am using author.php as an author page. I have changed the author base slug to profile so a post author can view their author page at website.com/profile/name

As these are basically personal profile pages, I don't want the public to see them so if you visit website.com/profile/name and are not logged in it should ask you to login. And if you are logged in and visit the URL of someone else's profile page it should redirect you back to your profile. I'm sort of stuck on this. I came across this in another post and I think it's a start:

add_action( 'template_redirect', 'wpse14047_template_redirect' );
function wpse14047_template_redirect()
  if ( is_author() ) {
    $id = get_query_var( 'author' );
    // get_usernumposts() is deprecated since 3.0
    $post_count = count_user_posts( $id );
    if ( $post_count <= 0 ) { 
        //include( STYLESHEETPATH .'/author-redirect.php' );
        wp_redirect( home_url() );

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 ) );


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



NOTE: code not tested, just written here

  • Nicely done - just tested it and works perfectly. I have a few questions for ya if you dont mind? why the esc_url_raw? in wp_redirect? – Coalition Partners Oct 5 '15 at 11:49
  • As said in codex, the esc_url_raw() function encodes the given URL but doesn't replace entities for display and it safe to use in redirections. That is why I use it whithin wp_redirect(). Anyway, I'm not sure if wp_redirect() already performs the url-enconde, in such case esc_url_raw() wouldn't be needed; I'm going to check it. Also, please, if this answers your question, mark the answer as accepted so other users can find the answer quickly (top-left corner, under the up-down vote buttons, there is a check mark). – cybmeta Oct 5 '15 at 11:56
  • I've checked wp_redirect() source code and it makes use of wp_sanitize_redirect() internally, so I think esc_url_raw() is not needed. But I'm not totally sure. Anyonw can confirm it? – cybmeta Oct 5 '15 at 12:00
  • Also, I need a similar setup for another a post. I have a CPT called submissions and I have created a add_rewrite_endpoint so I can show website.com/submissions/name-of-submission/edit-entry/ so the "edit-entry" is a loading edit-entry.php that I can allow for the editing of the entry. So in a similar fashion, the author that is viewing author.php and can now only view their own profile page, how could I make your code work to also only allow this post sub page to be viewed by its author? But, the single entry website.com/submissions/name-of-submission/ must be visible to all users. – Coalition Partners Oct 5 '15 at 12:03
  • It is difficult to answer in comments and also there is one-question per thread rule in this site. I tink it is better if you post a new question but before you could try to check if the endpoint query var is present instead of is_author() and then check current post author instead of current profile author. – cybmeta Oct 5 '15 at 12: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.