Sgtmullet
Sgtmullet

Reputation: 335

Wordpress - Adding a trailing slash when redirecting author pages to homepage

I have two Wordpress installs, one in the root (e.g www.example.com) and another in a folder (e.g. www.example.com/ask/).

I have already redirected all author pages to the root for the first one, but the second one ends up on e.g www.example.com/ask (notice no trailing slash). As a workaround, I was then adding a redirect for that to go to e.g. www.example.com/ask/, but chains are not ideal.

How can I use this code:

function disable_author_page() {
    global $wp_query;
    if ( $wp_query->is_author ) {
        wp_safe_redirect( get_bloginfo( 'url' ), 301 );
        exit;
    }

}
add_action( 'wp', 'disable_author_page' );

..and make it also add the trailing slash at the end?

Thanks!

Update: Would this work?

function disable_author_page() {
    global $wp_query;
    if ( $wp_query->is_author ) {
        wp_safe_redirect( $path = trailingslashit( get_bloginfo( 'url' ) ), 301 );
        exit;
    }

}
add_action( 'wp', 'disable_author_page' );

Upvotes: 0

Views: 392

Answers (1)

Aleksej
Aleksej

Reputation: 479

Try using this:

$url = home_url( '/' );
$redirect_url = esc_url( $url );

wp_safe_redirect( $redirect_url, 301 );

get_bloginfo('url') is a wrapper function for home_url(), ref: https://codex.wordpress.org/Function_Reference/home_url

As you can see in examples it will add slash on the end of url. So your final function will be like this:

function disable_author_page() {
    global $wp_query;

    if ( $wp_query->is_author ) {
       $url = home_url( '/' );
       $redirect_url = esc_url( $url );

       wp_safe_redirect( $redirect_url, 301 );

       exit;
    }

}

add_action( 'wp', 'disable_author_page' );

Hope this helps =)

Upvotes: 1

Related Questions