I’m working on a WordPress site where I need to customize the URL structure for user profiles. Right now I’m using author.php to display user information and everything works fine with the default setup.
The problem is I want to change the URL from /author/username to /profile/username but when I try to implement this change, I keep getting 404 errors when visiting the new URLs.
Here’s the code I’m using to make this change:
add_action('init', 'modify_user_permalinks');
function modify_user_permalinks() {
global $wp_rewrite;
$wp_rewrite->author_base = 'profile';
$wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '/%author%';
}
What am I missing to make this work properly? Do I need to flush rewrite rules or add something else to prevent the 404 errors?
i totally get it! i ran into a similar sitch too. like they said, add $wp_rewrite->flush_rules() but remember - only do that when really needed. also, go to your permalinks settings and just press save, it works like magic sometimes!
You’re on the right track but missing a key step. Your code modifies the rewrite structure but doesn’t regenerate the rewrite rules WordPress uses internally. I hit this exact same issue last year when customizing author URLs for a client. After adding your function, you need to flush the rewrite rules so WordPress recognizes the new structure. Quick fix: go to Settings > Permalinks in your admin and just click ‘Save Changes’ without changing anything. This forces WordPress to regenerate all rewrite rules. For a permanent solution, add flush_rewrite_rules() to your function, but be careful - it’s resource-intensive so only call it when needed. I usually hook it to theme or plugin activation, not on every page load. Also make sure your function runs early enough in the WordPress loading process - the ‘init’ hook works fine for this.
WordPress doesn’t recognize your custom rewrite structure until the rules are properly registered. I ran into this exact issue building a membership site with custom profile URLs. You’re on the right track modifying $wp_rewrite->author_base, but you need to make sure the rewrite rules actually update. Skip manually flushing rules every time - use add_rewrite_rule() instead for better control. Add this after your existing code: add_rewrite_rule('^profile/([^/]+)/?$', 'index.php?author_name=$matches[1]', 'top'); This explicitly tells WordPress how to handle your new URL pattern. Go to the permalinks page and hit save to activate changes. Watch out for caching plugins - they can mess with rewrite rules. Clear your cache if you’re still getting 404s after the fix.