feat: auto redirect wp-admin wp-login.php

This commit is contained in:
hanyixuanten
2026-07-31 16:18:22 +08:00
parent 156502ba77
commit ea65c7b93b
5 changed files with 59 additions and 2 deletions
+2
View File
@@ -154,6 +154,8 @@ When Japanese uses the `/ja/` subdirectory, its translated URL is:
https://example.com/ja/about/
```
Requests for `wp-login.php` or `wp-admin` through a target-language subdirectory or bound domain are redirected to the corresponding source-site URL. Query parameters are preserved.
The plugin prioritizes saved translations. It calls the translation API again only when the source content changes, a translation becomes invalid, or a user explicitly requests a refresh.
## Recommendations
+2
View File
@@ -154,6 +154,8 @@ https://example.com/en/about/
https://example.com/ja/about/
```
通过目标语言子目录或绑定域名请求 `wp-login.php``wp-admin` 时,插件会重定向到源站对应地址,并保留查询参数。
插件会优先使用已保存的译文。只有在原文发生变化、译文失效或用户主动要求刷新时,才需要重新调用翻译 API。
## 使用建议
+53
View File
@@ -6,12 +6,58 @@ class HTBD_Language_Router {
private $request_language = '';
public function register() {
$this->redirect_admin_request_to_source();
add_filter( 'query_vars', array( $this, 'query_vars' ) );
add_filter( 'do_parse_request', array( $this, 'strip_language_prefix' ), 1, 3 );
add_action( 'parse_request', array( $this, 'resolve_domain_language' ) );
add_filter( 'home_url', array( $this, 'localize_home_url' ), 20, 4 );
}
private function redirect_admin_request_to_source() {
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
$path = wp_parse_url( $request_uri, PHP_URL_PATH );
$query = wp_parse_url( $request_uri, PHP_URL_QUERY );
if ( ! is_string( $path ) ) {
return;
}
$relative_path = $this->relative_path( $path );
$segments = explode( '/', $relative_path, 2 );
$language = sanitize_key( $segments[0] );
$uses_language_path = HTBD_Settings::is_routing_mode_enabled( 'subdirectory' ) && in_array( $language, HTBD_Settings::subdirectory_languages(), true );
$uses_language_host = HTBD_Settings::is_routing_mode_enabled( 'domain' ) && $this->request_uses_bound_domain();
if ( ! $uses_language_path && ! $uses_language_host ) {
return;
}
if ( $uses_language_path ) {
$relative_path = isset( $segments[1] ) ? $segments[1] : '';
}
if ( ! preg_match( '#^(?:wp-login\.php|wp-admin)(?:/|$)#', $relative_path ) ) {
return;
}
$source_url = untrailingslashit( (string) get_option( 'home', '' ) );
if ( '' === $source_url ) {
return;
}
$redirect_url = $source_url . '/' . ltrim( $relative_path, '/' );
if ( is_string( $query ) && '' !== $query ) {
$redirect_url .= '?' . $query;
}
wp_redirect( esc_url_raw( $redirect_url ), 302, 'HTBD' );
exit;
}
public function register_rewrite_rules() {
// Subdirectory requests reuse WordPress's existing rewrite rules after their language prefix is removed.
}
@@ -136,6 +182,13 @@ class HTBD_Language_Router {
return '' !== $host && isset( $bindings[ $host ] ) && sanitize_key( $bindings[ $host ] ) === $language;
}
private function request_uses_bound_domain() {
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
$bindings = (array) HTBD_Settings::get()['domain_bindings'];
return '' !== $host && isset( $bindings[ $host ] ) && HTBD_Settings::is_supported_language( $bindings[ $host ] );
}
private function source_origin_parts( $parts ) {
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
$bindings = (array) HTBD_Settings::get()['domain_bindings'];
+1 -1
View File
@@ -18,7 +18,7 @@ WordPress 后台界面会跟随当前 WordPress 语言:默认显示英文,Wo
支持的内容包括已发布文章和页面的标题、正文、摘要、图片替代文本、分类、标签及部分 SEO 字段。插件只为 `publish` 状态的文章和页面自动安排翻译。完成的翻译保存在 `{$wpdb->prefix}hyx_bd_translations` 自建数据表中;启用改名后的插件时,已有 `{$wpdb->prefix}btranslate_translations` 数据会自动迁移。前端渲染只读取已保存的翻译,不会动态请求翻译服务。
子目录路由支持 `/en/example-post/` 等路径。域名路由可将配置的主机名映射到目标语言。翻译失败不会中断页面请求;没有有效译文时会回退显示源语言内容。
子目录路由支持 `/en/example-post/` 等路径。域名路由可将配置的主机名映射到目标语言。通过目标语言路由请求 `wp-login.php`、`wp-admin` 时,插件会重定向到源站对应地址并保留查询参数。翻译失败不会中断页面请求;没有有效译文时会回退显示源语言内容。
== Installation ==
+1 -1
View File
@@ -18,7 +18,7 @@ The WordPress admin interface follows the current WordPress locale. English is t
Supported values include published post and page titles, content, excerpts, image alternative text, categories, tags, and selected SEO fields. Automatic translation is only scheduled for posts and pages with the `publish` status. Completed translations are stored in the `{$wpdb->prefix}hyx_bd_translations` custom database table. Existing `{$wpdb->prefix}btranslate_translations` data is migrated when the renamed plugin is activated. Front-end rendering reuses stored values and never calls the translation provider dynamically.
Subdirectory routing supports paths such as `/en/example-post/`. Domain routing maps configured hostnames to target languages. Translation failures are non-fatal and fall back to source-language content.
Subdirectory routing supports paths such as `/en/example-post/`. Domain routing maps configured hostnames to target languages. Requests for `wp-login.php` or `wp-admin` through a target-language route are redirected to the corresponding source-site URL while preserving query parameters. Translation failures are non-fatal and fall back to source-language content.
== Installation ==