How to change user (author) page slug without plugins

PubDate:

/ ModDate:

WordPress Customize Ideas | Personal WP Customization Notes (PWCN)

<Description based on the Act on Premiums and Representations> The content on this site may include product promotions.

Anyone who is creating a site with WordPress and has even a little knowledge or interest in login security is aware of the problem where the slug (the end of the URL) on the user (author) page becomes the username.

Try entering a URL in the form below directly on your own site, or click View from the user list to open it.

Site top page URL/author/username

…If you don’t take any precautions, your username will be exposed.

This “username” part is called a slug, and there is a famous plug-in called “Edit Author Slug” that allows you to freely change the slug.

However, unless your site has a large increase or decrease in users (such as a membership site), you won’t need a plugin feature that automatically changes the slug upon user registration, so I think it would be a good idea to add the feature yourself.

Therefore, I will introduce a code that allows you to freely change the author slug from the user profile editing screen by adding code to the theme’s functions.php.

If you have never added or changed your theme’s functions.php, it is better to just use a plugin.

Code that allows you to change the author slug

Let me first explain what I’m doing. I haven’t analyzed the code, but the previous plugin probably does the same thing.

The author slug data is stored in the item “user_nicename” in the user database table.

If you do nothing at the time of user registration, this item will be set to the same value as the user ID (user name), so the author slug will become the user ID.

The code below adds an item called “Author slug” to the user profile screen, and by changing this item, you can freely change the author slug (red frame in the image below).

How to change user (author) page slug without plugins|Personal WP Customization Notes (PWCN)

Also, although few people may notice, with the “Edit Author Slug” plugin, unless you restore and update the slug settings and then stop or delete the plugin, the changed author slug settings will remain as they are. It will be.

Therefore, in the code below, if you leave the “Author slug” field empty (blank) and update it, the original user ID will be restored.

Below is the code to do this. Please add it to functions.php of the enabled theme.

if (current_user_can('manage_options')) {

function ha_author_slug_edit_ob_start() {
	ob_start();
}
add_action( 'personal_options', 'ha_author_slug_edit_ob_start' );

function ha_insert_author_slug_input( $user ) {
    $content = ob_get_clean();
    $regex = '/<tr(.*)class="(.*)\buser-user-login-wrap\b(.*)"(.*)>([\s\S]*?)<\/tr>/';
    $nicename_row = sprintf(
        '<tr class="user-user-nicename-wrap"><th><label for="user_nicename">%1$s</label></th><td><input type="text" name="user_nicename" id="user_nicename" value="%2$s" class="regular-text" />' . "\n" . '<span class="description">%3$s</span></td></tr>',
        esc_html__( 'author slag(Optional)' ),
        esc_attr( $user->user_nicename ),
        esc_html__( 'Please enter the author slug using half-width alphanumeric characters, "-" and "_"' )
    );
    echo preg_replace( $regex, '\0' . $nicename_row, $content );
}
add_action( 'show_user_profile', 'ha_insert_author_slug_input' );
add_action( 'edit_user_profile', 'ha_insert_author_slug_input' );

function ha_author_slug_profile_update( $errors, $update, $user ) {
    if ( !$update ) return;
    if ( empty( $_POST['user_nicename'] ) ) {
        $user->user_nicename = $_POST['user_login'];
    } else {
        $user->user_nicename = $_POST['user_nicename'];
    }
}
add_action( 'user_profile_update_errors', 'ha_author_slug_profile_update', 10, 3 );
}

I can’t believe it can be implemented with just this much code. .. .. That’s what I feel, but that’s all (laughs).

After adding, actually change the “author slug”,

Site top page URL/author/author slag

The author’s post list page will be displayed, and

Site top page URL/author/username

Check if you get a 404 error (page not found).

reference:How to Change Author URL Slug and Base with or without Plugin


This page is an English translation of the Japanese page. The information on the Japanese page may be more accurate because we maintain the Japanese page as the basis and not all content matches.
You can view the Japanese page by clicking the “View Japanese Page” button on the top right of the page, so please take a look.

If you found the content posted on this page or the reference code introduced helpful, please leave a comment in the comment section at the end of the page, or spread the word on SNS etc.

The methods and codes posted on this page have been confirmed to work in my environment as of the last update date. Please note that in principle, we will not respond even if we receive information about malfunctions or detailed customization methods in the comments section.

Until the end Thank you for reading.

The copyright and ownership of all content published on this page, including texts, images, and codes, belongs to this site, and reproduction is strictly prohibited.