How to Change the Author Base in WordPress

The Author Base is the middle part of the Author URL, by default it is /author/. My Author URL on this site is https://letswp.io/author/attila/.

For some reason, unlike the category or tag bases, WordPress doesn’t give us an option to change it on Settings Permalinks Screen (Settings > Permalinks admin page), so we need to find an alternative way.

In this article, I will show you two possible approaches to change the Author Base programmatically. In the first snippet, we will define the new base in our source code. Then, giving it more flexibility, we will perform the modification dynamically via an input field on the Permalinks page.

Rewriting the URL

When it comes to changing the Author Base, we should modify the $wp_rewrite global variable. It’s a single instance of the WP_Rewrite class, which WordPress uses for managing URL rewrite rules. Thanks to this our WordPress powered site has SEO friendly Pretty Permalinks.

Code #1 – Hardcoding the Author Base

/**
 * Custom Author Base
 *
 * @return void
 */
function lwp_2610_custom_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base = 'user';
}
add_action( 'init', 'lwp_2610_custom_author_base' );

In the code above, we simply assign a string to the author_base property of the $wp_rewrite object, for example “user” or “profile”. Remember, it’s part of an URL, so don’t use any special characters or spaces.

Let’s see what happens after applying the code and try to reach our brand new Author URL. We’ll get a “Page Not Found” error. To make this change permanent, we should rebuild the permalink structure. Navigate to the Permalinks admin screen and click to Save button.

Note: We can rebuild the permalink structure programmatically as well, by calling the flush_rewrite_rules() function. It’s a very costly operation, so be sure to call it only once. 

As shown, this method consists of 2 steps: creating the code itself then rebuilding the permalink structure. Wouldn’t be more convenient to perform it only on the admin page? All right. Let’s extend our code.

Code #2 – Change the Author Base Dynamically

The following snippet allows the users to change the Author Base themselves via an input field on the Permalinks admin screen. It’s a more comfortable way of modification.

/**
 * Rewrite author base to custom
 *
 * @return void
 */
function lwp_2610_author_base_rewrite() {
    global $wp_rewrite;
    $author_base_db = get_option( 'lwp_author_base' );
    if ( !empty( $author_base_db ) ) {
        $wp_rewrite->author_base = $author_base_db;
    }
}

add_action( 'init', 'lwp_2610_author_base_rewrite' );

/**
 * Render textinput for Author base
 * Callback for the add_settings_function()
 *
 * @return void
 */
function lwp_2610_author_base_render_field() {
    global $wp_rewrite;
    printf(
        '<input name="lwp_author_base" id="lwp_author_base" type="text" value="%s" class="regular-text code">',
        esc_attr( $wp_rewrite->author_base )
    );
}

/**
 * Add a setting field for Author Base to the "Optional" Section
 * of the Permalinks Page
 *
 * @return void
 */
function lwp_2610_author_base_add_settings_field() {
    add_settings_field(
        'lwp_author_base',
        esc_html__( 'Author base' ),
        'lwp_2610_author_base_render_field',
        'permalink',
        'optional',
        array( 'label_for' => 'lwp_uthor_base' )
    );
}

add_action( 'admin_init', 'lwp_2610_author_base_add_settings_field' );

/**
 * Sanitize and save the given Author Base value to the database
 *
 * @return void
 */
function lwp_2610_author_base_update() {
    $author_base_db = get_option( 'lwp_author_base' );

    if ( isset( $_POST['lwp_author_base'] ) &&
        isset( $_POST['permalink_structure'] ) &&
        check_admin_referer( 'update-permalink' )
    ) {
        $author_base = sanitize_title( $_POST['lwp_author_base'] );

        if ( empty( $author_base ) ) {
            add_settings_error(
                'lwp_author_base',
                'lwp_author_base',
                esc_html__( 'Invalid Author Base.' ),
                'error'
            );
        } elseif ( $author_base_db != $author_base ) {
            update_option( 'lwp_author_base', $author_base );
        }

    }
}

add_action( 'admin_init', 'lwp_2610_author_base_update' );

In the code above, we use the  add_settings_field() function of the Settings API to create the input field. The lwp_2610_author_base_update() function (that is hooked to the admin_init action) handles the updates. Typically, the register_setting() API function would be responsible for this, but it doesn’t work on the Permalinks page. It perfectly does its job on other setting pages, like General or Reading, but not here. Our custom function is a workaround for the issue. It does everything that the register_settings() function is supposed to do, including error handling, sanitizing and saving data into the options database table. Similarly to the first approach, the essence of the code is to modify the $wp_rewrite global object. But this time, the value of the author base comes from the database.

After applying the snippet, a new text input appears at the bottom the Options section of the Permalinks page.

Profile Input

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Our Newsletter!

Get secret tips and valuable details about making your site more successful.

Follow Us!

We Recommend

This site is powered by Elementor

100% Free  WP Website Builder  All-in-One Solution

Monthly Archives

Scroll Up