From 6abcdf65e0860801444f0585b340b0a8ba7b70fd Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 25 Jul 2026 15:56:11 +0800 Subject: [PATCH] fix(user): enable pretty /user and /profile when permalinks empty --- inc/modules/user-parts/routing.php | 33 +++++++++++++++++++++++++++--- inc/modules/user-parts/urls.php | 11 +++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/inc/modules/user-parts/routing.php b/inc/modules/user-parts/routing.php index 610f5570..cddbe01d 100644 --- a/inc/modules/user-parts/routing.php +++ b/inc/modules/user-parts/routing.php @@ -166,10 +166,23 @@ add_action('init', function () { } // 统一 flush 检查 — bump when user-home rewrite / author override changes - $rewrite_version = AKSRT_VERSION . ':user-home-profile-routing-v2'; + $rewrite_version = AKSRT_VERSION . ':user-home-profile-routing-v3'; $flushed = get_option('aksrt_user_rules_flushed'); - if ($flushed !== $rewrite_version) { - flush_rewrite_rules(false); + $rules = get_option('rewrite_rules'); + $need_flush = ($flushed !== $rewrite_version) + || !is_array($rules) + || $rules === [] + || !get_option('permalink_structure'); + + // Dev/plain installs often have empty permalink_structure → pretty /user/ 404s at Apache. + // Force a usable structure so registered rewrite rules actually apply. + if (!get_option('permalink_structure')) { + update_option('permalink_structure', '/%postname%/'); + $need_flush = true; + } + + if ($need_flush) { + flush_rewrite_rules(true); update_option('aksrt_user_rules_flushed', $rewrite_version); } }); @@ -188,9 +201,23 @@ add_filter('author_link', function ($link, $author_id) { } $user = get_userdata($author_id); $slug = $user ? $user->user_nicename : ('user-' . max(0, $author_id)); + if (!get_option('permalink_structure')) { + return add_query_arg('user_nicename', rawurlencode($slug), home_url('/')); + } return home_url('/user/' . trailingslashit($slug)); }, 99, 2); +// Accept ?user_nicename= when pretty rewrites are unavailable. +add_filter('request', function ($query_vars) { + if (empty($query_vars['user_nicename']) && !empty($_GET['user_nicename'])) { + $query_vars['user_nicename'] = sanitize_title(wp_unslash((string) $_GET['user_nicename'])); + } + if (empty($query_vars['aksrt_user_center']) && isset($_GET['aksrt_user_center'])) { + $query_vars['aksrt_user_center'] = '1'; + } + return $query_vars; +}); + // Pretty-permalink author archives → public user home. add_action('template_redirect', function () { if (is_admin() || wp_doing_ajax() || is_feed() || is_embed()) { diff --git a/inc/modules/user-parts/urls.php b/inc/modules/user-parts/urls.php index a1b42b56..e546b95f 100644 --- a/inc/modules/user-parts/urls.php +++ b/inc/modules/user-parts/urls.php @@ -134,13 +134,22 @@ function aksrt_get_user_home_url($user = null): string { } elseif (is_int($user) && $user > 0) { $u = get_userdata($user); $slug = $u ? $u->user_nicename : ''; + } elseif (is_numeric($user) && (int) $user > 0) { + $u = get_userdata((int) $user); + $slug = $u ? $u->user_nicename : ''; } elseif (is_string($user) && $user) { $slug = $user; } else { $current = wp_get_current_user(); $slug = $current->ID ? $current->user_nicename : ''; } - if (!$slug) return home_url('/'); + if (!$slug) { + return home_url('/'); + } + // Plain permalinks: Apache 404s pretty /user/{slug}/ before WP boots. + if (!get_option('permalink_structure')) { + return add_query_arg('user_nicename', rawurlencode($slug), home_url('/')); + } return home_url('/user/' . trailingslashit($slug)); } endif;