Last updated on September 25, 2023

Change the Author URL Base

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Improve branding by changing author URLs.

To change the author URL base in WordPress, you can use the author_rewrite_rules filter hook along with the add_rewrite_rule() function. This allows you to modify the URL structure for author archives.

Here’s an example code snippet that demonstrates how to change the author URL base to “members”:

function wpsnippets_change_author_url_base() {
    add_rewrite_rule(
        '^members/([^/]+)/?',
        'index.php?author_name=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpsnippets_change_author_url_base' );

In this code, we define a custom rewrite rule using the add_rewrite_rule() function. The first parameter is the regular expression pattern that matches the desired URL structure. In this case, it matches URLs starting with “members/” followed by the author’s username.

The second parameter is the query string that WordPress should use to retrieve the author’s posts. We use the author_name query variable to specify the author’s username.

The third parameter, 'top', ensures that this rewrite rule takes precedence over other rules.

By adding this code to your theme’s functions.php file or a custom plugin, you can change the author URL base to “members”. After saving the changes, make sure to flush the rewrite rules by visiting the “Settings” > “Permalinks” page in your WordPress admin area.

Now, author archive URLs will be structured like example.com/members/username/, where “username” is the author’s username.

This code snippet can be useful if you want to customize the author URL structure to match your site’s branding or SEO requirements.

Examples

Example 1: Changing the author URL base using a filter hook

This example demonstrates how to change the author URL base in WordPress using the author_rewrite_rules filter hook.

function wpsnippets_change_author_url_base( $author_rewrite ) {
    $author_rewrite['author_base'] = 'members';
    return $author_rewrite;
}
add_filter( 'author_rewrite_rules', 'wpsnippets_change_author_url_base' );

In this code example, we define a custom function wpsnippets_change_author_url_base that modifies the $author_rewrite array by changing the value of the author_base key to 'members'. We then add this function as a filter to the author_rewrite_rules hook. This will change the author URL base from the default 'author' to 'members'.

Example 2: Changing the author URL base using a plugin

This example demonstrates how to change the author URL base in WordPress by creating a custom plugin.

/*
Plugin Name: Custom Author URL Base
*/

function wpsnippets_change_author_url_base( $author_rewrite ) {
    $author_rewrite['author_base'] = 'members';
    return $author_rewrite;
}
add_filter( 'author_rewrite_rules', 'wpsnippets_change_author_url_base' );

In this code example, we create a custom plugin by creating a new PHP file with the plugin header comment. Inside the plugin file, we define the wpsnippets_change_author_url_base function, which is the same as in the previous example. We then add this function as a filter to the author_rewrite_rules hook. When the plugin is activated, it will change the author URL base to 'members'.

Example 3: Changing the author URL base using a mu-plugin

This example demonstrates how to change the author URL base in WordPress by creating a must-use plugin (mu-plugin).

<?php
/*
Plugin Name: Custom Author URL Base
*/

function wpsnippets_change_author_url_base( $author_rewrite ) {
    $author_rewrite['author_base'] = 'members';
    return $author_rewrite;
}
add_filter( 'author_rewrite_rules', 'wpsnippets_change_author_url_base' );

In this code example, we create a must-use plugin by creating a new PHP file and placing it in the wp-content/mu-plugins directory. Inside the plugin file, we define the wpsnippets_change_author_url_base function, which is the same as in the previous examples. We then add this function as a filter to the author_rewrite_rules hook. The mu-plugin will automatically be loaded and activated, changing the author URL base to 'members'.

Last updated on September 25, 2023. Originally posted on October 12, 2023.

Leave a Reply