This site is running Voxel Theme Version: 1.5.5.1 | Elementor Plugin Version: 3.28.4

Join the Voxel Guide Community!

Get Involved

Change profile slug from display name to author id

Description/Instructions

As described below. Please use it carefully. Backup your site first !

Instructions

Change profile slug from display name to author_id (eg: site/profile/johnsmith/ to site/profile/122/)

1. Backup your site

2. Add the following php code to child theme functions.php file or using a code snippets plugin.

3. Go to Settings > Permalinks > Press Save changes twice.

function custom_author_rewrite_rules() {
add_rewrite_rule('^profile/([0-9]+)/?$', 'index.php?author=$matches[1]', 'top');
}
add_action('init', 'custom_author_rewrite_rules');

function change_author_link_to_id($link, $author_id, $author_nicename) {
return home_url('/profile/' . $author_id);
}
add_filter('author_link', 'change_author_link_to_id', 10, 3);

function redirect_old_author_slug_to_id() {
if (is_author()) {
$author = get_queried_object();
$current_url = home_url($_SERVER['REQUEST_URI']);
$correct_url = home_url('/profile/' . $author->ID);

if (trailingslashit($current_url) !== trailingslashit($correct_url)) {
wp_redirect($correct_url, 301);
exit;
}
}
}
add_action('template_redirect', 'redirect_old_author_slug_to_id');

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24