Opened 11 months ago
Last modified 7 months ago
#21143 new enhancement
Add a hook for when no blog is found (in addition to NOBLOGREDIRECT)
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Multisite | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: |
Description
(Related to #21142)
Background
On a multisite install, we allow users to change their subdomain name (or domain name).
We keep a list of their previously used subdomains/domains, and if there is a HTTP request for one of their old domains, we automatically perform a 301 redirect to the new domain name.
Problem
In order to perform a 301 redirect from and old domain to the new domain, we currently have to use wp-content/sunrise.php.
However we would like to take advantage of the logic at the top of wp-includes/ms-settings.php, rather than re-inventing that logic in our sunrise.php file.
The NOBLOGREDIRECT constant can be used for this, but it does not allow us to perform a 301 redirect.
Solution
Adding a simple hook to ms-settings.php would allow a sunrise.php file to determine what should happen if a HTTP request on a Multisite install does not correspond to one of the existing sites/blogs.
See attached patch.
Attachments (4)
Change History (12)
jamescollins — 11 months ago
jamescollins — 11 months ago
comment:1
jamescollins — 11 months ago
The new 'no_blog_redirect' hook would fire before mu-plugins are initialised, which means that a custom function would have to hook into 'no_blog_redirect' by using sunrise.php.
The attached sunrise.php file is a simple example that demonstrates how this new hook could be used.
The end result is a relatively simple way to add custom logic that only gets executed for requests that don't match an existing blog/site on a Multisite install.
I don't think 'no_blog_redirect' is an appropriate name. Maybe I want to do something else when a blog is not found.
So, how about 'blog_not_found' instead?
comment:3
in reply to:
↑ 2
jamescollins — 11 months ago
Good suggestion scribu.
New patch adds a 'blog_not_found' hook instead.
- Keywords needs-patch punt added; has-patch removed
The variables in that file are a bit confusing. $domain, $blogname, $current_site, $current_blog, $path
Looks like the most important ones there are $domain and $path, along with $current_site and $blogname (which is either a piece of the domain, or a piece or all of the path). $current_blog is going to be null, and if no blog was found, then passing it doesn't really make any sense.
If we're going to add an action here, we should probably add it to the next block too, which handles non-subdomain can't-find-the-blog situations.
I really want to just rewrite ms-settings.php one of these days. Since this can be handled with sunrise.php (though fairly annoying to do), I'm tempted to punt, unless we can come up a patch that consistently fires this hook for every time we can't find a site. (These are not the only two instances.)
jamescollins — 8 months ago
comment:6
jamescollins — 8 months ago
- Keywords has-patch added; needs-patch removed
Thanks for your input Andrew. I agree that the code is in need of a rewrite!
21143_2.diff is another attempt at this.
Rather than duplicate the same do_action() code in multiple places, I've elected for a single call further up in the code.
The parameters for the hook have also been cleaned up, as per your suggestions.
It should now fire for both subdomain and subdirectory installs, and it'll also fire before NOBLOGREDIRECT comes into play.
- Milestone changed from 3.5 to Future Release
I want to revisit this in 3.6. I think I might finally rewrite ms-settings.php then, and I don't want an action to make it more difficult to basically start from scratch (because it is terrible). This can still be handled in some form in sunrise.php and through more hacky means, so I don't feel too bad about leaving it as is for now.
comment:8
SergeyBiryukov — 7 months ago
- Keywords punt removed

Example wp-content/sunrise.php file that uses the new 'no_blog_redirect' hook