8000 feat: Rewrite admin URLs · nlemoine/wp-symfony-local-server@00f136c · GitHub
[go: up one dir, main page]

Skip to content

Commit 00f136c

Browse files
committed
feat: Rewrite admin URLs
1 parent 6218120 commit 00f136c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ A set a hooks to fix running WordPress on [Symfony Local Server](https://symfony
44

55
Currenlty solve:
66
- Self requests (`wp_remote_get('https://domain.wip/page/')`): send them through Symfony local proxy so local TLD is resolved and set certificate to avoid SSL errors
7+
- Rewrite admin URLs: https://domain.wip/wp-admin/ -> https://domain.wip/wp-admin/index.php (to avoid redirects as much as possible)
78
- Admin redirects: https://domain.wip/wp-admin/ -> https://domain.wip/wp-admin/index.php
89
- Automatic WP_HOME constant set according to local domain
910

src/bootstrap.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,30 @@ function redirectWpAdminStatus($status, $location): int
215215

216216
return admin_url('index.php') === $location ? 302 : $status;
217217
}
218+
/**
219+
* Adds index.php to the admin URL if no path is specified.
220+
*
221+
* @param string $url The complete admin area URL including scheme and path.
222+
* @param string $path Path relative to the admin area URL. Blank string if no path is specified.
223+
* @param int|null $blog_id Site ID, or null for the current site.
224+
* @param string|null $scheme The scheme to use. Accepts 'http', 'https',
225+
* 'admin', or null. Default 'admin', which obeys force_ssl_admin() and is_ssl().
226+
*/
227+
function rewriteAdminUrl($url, $path, $blog_id, $scheme): string
228+
{
229+
$pathParts = parse_url($path);
230+
// It already has a path
231+
if (! empty($pathParts['path'])) {
232+
return $url;
233+
}
234+
235+
return empty($path) ? sprintf('%s/index.php', rtrim($url, '/')) : str_replace($path, sprintf('index.php%s', $path), $url);
236+
}
218237

219238
if (isSymfonyLocalServer()) {
220239
earlyAddFilter('pre_http_send_through_proxy', __NAMESPACE__ . '\\shouldSendThroughProxy', 10, 4);
221240
earlyAddFilter('https_ssl_verify', __NAMESPACE__ . '\\verifySsl', 10, 2);
222-
earlyAddFilter('redirect_canonical', __NAMESPACE__ . '\\redirectWpAdmin', 10, 2);
241+
earlyAddFilter('redirect_canonical', __NAMESPACE__ . '\\redirectWpAdmin', PHP_INT_MAX, 2);
223242
earlyAddFilter('wp_redirect_status', __NAMESPACE__ . '\\redirectWpAdminStatus', 10, 2);
243+
earlyAddFilter('admin_url', __NAMESPACE__ . '\\rewriteAdminUrl', PHP_INT_MAX, 4);
224244
}

0 commit comments

Comments
 (0)
0