Redirect the main site of a WordPress multisite network

Before I start with my first „Project 52“ blog post, I want to apologize to all my English readers for not being version active. If you don’t know German, you didn’t have too much to read on my blog in the past. It was acutaly only one real post in the last 4 years (and three posts with slides from my WordCamp talks) only. But this is going to change now!

For those of you who can read German, you might have read my advent calendar in 2015, followed by #projekt52 (a blog post every week of the year) and another advent calendar in 2016. As I was very unhappy with the fact, that I haven’t blogged in English too much, I’ve made a decision: I will blog every two weeks in English and on every other week, I’ll translate this blog post into German, starting today!

A multisite with no main site

This week, I finished the migration of a large client project into a new multisite installation. This multisite had the goal to have one sub-site per customer of our client. Every multisite needs a main top level domain. For this example, let’s say the name of the domain was abc-customers.com. The multisite was set up to use subdomains, while working on the draft of a new customer sub-site. So, we would have something like somecustomer.abc-customers.com and anothercustomer.abc-customers.com. Once the sites where ready to go live, the sub-site were switched to a domain or subdomain (e.g. abc.somecustomer.com and anothercustomer-platform.com). I was using the core WordPress functionality to map those domains and no additional domain mapping plugin.

This all works fine. Any URL on those sites would only have the mapped domain names and the subdomain used by the sub-site before were no longer visible. Still there were some issue with that. First of all, the domain abc-customers.com was still a regular sub-site of the multisite network. And secondly, a call to any random subdomain would redirect to the „site registration page“ (the public registration was disabled) and the main domain was visible again.

Using a blank/maintenance page

I first thought about using a blank page for domain of the main site. But I couldn’t find a nice „blank theme“ for that page and I actually didn’t like the idea of showing the visitor „nothing“ and asking him to click on some link to go somewhere else. So, I thought, a redirect would be a lot better.

Redirecting the whole domain

The first idea one of my colleagues had, was redirecting the whole abc-customers.com domain to another domain. But this is not going to work, as the domain is mandatory to be able to access the network administration for the multisite installation.

Frontend redirects only

So, we needed to limit the redirect only to the frontend pages of the main site. In order to solve this, I came up with a very small plugin, that will do those redirects:

function multisite_mainsite_redirect() {
	if ( is_main_site() && ! is_admin() ) {
		wp_redirect( 'https://example.com/' );
		exit();
	}
}
add_action( 'init', 'multisite_mainsite_redirect' );

Looks easy, eh? As it turned out, it was a bit too easy. There are some edge cases that needs to be taken into account.

The registration page

Even if you activate the plugin for the whole multisite network, it will not catch all pages. One page it was not working on, was the registration page for new multisite sub-sites. Even if you deactivate the public registration, the page will still render showing a message with a notice. So, I had to add a condition to redirect that page as well (I’ll show you the code in the result in a bit).

Conflicts with WP-CLI

After changing the conditions in the plugin, I thought I would have solved all cases. But while migration the sites from the staging server, I used the WP-CLI to update all URLs to the mapped domains. On any WP-CLI command, I got a warning and the command was not executed:

$ wp
Warning: Some code is trying to do a URL redirect. Backtrace:
#0 WP_CLIUtilswp_redirect_handler(http://example.com/)
...

To get the plugin work with the WP-CLI, I had to use another additional condition.

The result

After the those changes, I ended up with this little function:

function multisite_mainsite_redirect() {
	if ( is_main_site() && ! is_admin() && 'wp-login.php' != $GLOBALS['pagenow'] && ! defined( 'WP_CLI' ) ) {
		wp_redirect( 'http://example.com/' );
		exit();
	}
}
add_action( 'init', 'multisite_mainsite_redirect' );

The function is using a static redirect. But if you would like to redirect to another sub-site or a dynamic URL, that could also easily be done in the function.

Conclusion

I’ll hope you find that little plugin useful. As usually, I’ve published the plugin code as a GIST where you can simply download it as a ZIP file and install it to your site. If you have any additional questions, please leave a comment below.

And I also hope you liked the first blog post of my little „bilingual fortnightly blog posting challenge“ 😉 If you have any topics, you would like me to blog about, please share your ideas.

Hopefully this first blog post after quite a long time didn’t had too many typos or grammatical errors 😉

WordCamp Bilbao 2016 – A WordCamp not like many others

The last two days I attended the first WordCamp in Bilbao, Spain. It was not at all the first WordCamp outside of Germany for me but I still experienced some differences. The first thing was, that I attended only two Sessions. Or at least only two I understood. Because all the other sessions were given in Spanish or Basque, which I both don’t speak 🙂 And that brings me to the first thing I liked about this WordCamp.

A WordCamp truly for the local community

The idea of WordCamps started as something local. We tend to forget this as we attend big ones like WordCamp in London with 600+ and the WordCamp Europe this year with 2200+ attendees. So they are usually very international and it is pretty common that people travel to those WordCamps from all over Europe (or even worldwide).

I had the WordCamp in Bilbao on my list because I wanted to visit a very good friend in this town, while also attending the WordCamp. So as soon as they announced the date, which was not even two months before the WordCamp, I asked if there will be any English talks. Ibon Azkoitia, the lead organizer, reached out to me on Slack and told me that they were planning to have 80% local speakers and 20% foreign. And with „local“ they really meant local speakers, so from Bilbao and around. And this also meant some talks in Basque, the local language, which is just a Spanish dialect but a very own language with no connection to any other language.

He then asked me to give him the title of the talk I was planning to give 🙂 I really didn’t mean to give a talk, I just wanted to visit the WordCamp as well as the city and my friend. But he asked so nicely, how could I have say no? 😉

Weiterlesen →

Slides from my session „Better Themes with Scalable Vector Graphics (SVG)“ at WordCamp Nuremberg 2016

My first WordCamp talk this year about a topic I personally find really important. I hope that you have all learned something new. At least the bit about CSS filters 🙂

Better Themes with SVG

I also added an „Online Image Map Generator“ service to the sources, that looks quite nice. For the Color-Matrix CSS filters, I couldn’t find an easy generator.

UPDATE 11.06.2016: I’ve also givin this talk at WordCamp Bilbao. My slightly modfied slides can also be here: Better Themes with SVG – WordCamp Bilbao 2016.

Slides from my session „A Brief Word on Plugin Initialization“ at WordCamp Berlin 2015

At my seventh and last WordCamp for this year, I was part of the organizing team and not only a regular attendee. But I still found the time to prepare a lightning talk. It was about a topic I see a lot while working on client websites. So here are the slides of the session:

A Brief Word on Plugin Initialization

I had the feeling, that some attendees got the point of my talk. If you know any more modern way to initialize a plugin or if you have any further questions, just leave me a comment.

Slides from my session „Beginner’s Guide to SASS“ at WordCamp Norrköping 2015

Today I was able to held my session about SASS, which I first presented in June at the WordCamp Cologne, in English at the WordCamp Norrköping, Sweden. In this post you can find the slides I used including the sources and links. Feel free to view or download it:

Beginner´s Guide to SASS

If you have any additional questions after my session, just leave me a comment. I would also love to hear your feedback on it.

But now it’s time for the last session followed by a great afterparty and a Contributor Day tomorrow.

UPDATE 12.06.2016: I’ve also givin this talk at the WordCamp Bilbao Contributor Day. My slightly modfied slides can also be here: Beginner’s Guide to SASS – WordCamp Bilbao Contributor Day 2016.

Use WP-Minify and Jetpack together

Like probably the most of you, I use the Jetpack plugin from Automattic. Some time ago I also installed the plugin WP-Minify. This plugin enables you to combine all JavaScript and CSS files from the HEAD into a single file which can also be minified. In doing so you are saving your visitors a lot of requests. Also the total request size will decrease because of the minification. If you haven’t set up some caching headers fpr JavaScript and CSS, the plugin will also do this for you. Unfortunately, there are problems with the simultaneous use of Jetpack and WP-Minify. But the solution is quite simple.

Weiterlesen →

Setup a catchall email account in Plesk

As I mentioned in several other articles this blog running is running on a server with Plesk. For my domain I can currently specify only one e-mail address for the domain at my hoster. But since I wanted to use other addresses I took advantage of the Plesk email function.

Since all email addresses of my domain would be forwarded to a single address anyways, I still wanted find a way in Plesk and finally found it. It’s failry easy to setup a so-called catchall email account. The setting, however, is a bit hidden.

Weiterlesen →

In April still a joke, but now it’s reality!

-In April I wrote about the brilliant April Fool’s joke of tagesschau.de. It reported that the last IP address was assigned and due to maintenance all DNS root servers must be shut down for 24 hours.

Now it is actually so far. They won’t shut down all dedicated server for 24 hours, but the last IPv4 addresses have been assigned (to be precise, the last five code blocks have been awarded) as you can read on tagesschau.de.

But what does this mean? Do we all need to panic? No we do not need to. With IPv6 we there has been a successor for a long time, but it hasn’t been fully enforced, yet. Many ISPs still haven’t converted their entire infrastructure.

I am so excited with which April’s Fools joke the media will come up this year, and how long does it take until it is sad reality.

Addendum:

Now the time has come for the 24-hour shutdown, as golem.de reported. However only large internet companies like Google, Facebook will test their services for 24 hours on IPv6 infrastructure. We will see whether everything still runs smoothly for IPv4 users. The IPv6 Day is planed for June, 8 2011.

  • Veröffentlicht in: Misc

Shortcut of the month: CTRL + SHIFT + T (Aptana)

A few days ago I was working with eclipse (or more precisely with Aptana 1.5) on a project. I have accidentally closed a file, although I was not finished yet. In trying to open this file again I could have go to the „Project View“ open it through the directory list.

The Shortcut

We know this shortcut from many other programs to open a recently closed window/tab. So why shouldn’t it work in eclipse? So I just tried to reopen the file useing the shortcut:

CTRL + SHIFT + T

Weiterlesen →

Happy new year and a small shortcut

I wish all my regular readers (and those who want to be) a happy new year 2011. Fortunately, the Jugendmedienschutz-Staatsvertrag has not been adopted, so that I can still safely tell you about interesting topics related to web development.

One of my regular readers is my colleague Michael, who has pointed me on a new shortcut in the office and asked me if it would be worthy of a „shortcut of the month“. He is very handy, but I’m sorry Michael, there are 2 alternatives to it, so he will not make it to a shortcut of the month. But I want to introduce him anyway:

ALT + ESC

With this shortcut you can cycle through the open programs in Windows without seeing the previews of the corresponding program. Strictly speaking, the currently active window moves to the end of the z-order, so it can’t simple be switched back to the previous program by using the shourtcut again.

Because it changes so the usual behavior when switching between programs and since Windows Vista introduced the great „Flip 3D“ function, the shortcut is probably used only by very few (if they know him at all). A detailed overview of the program switch shortcuts can be found on the Alt-Tab page in the English Wikipedia.

Now I wish once again a happy new year and enjoy reading my other blog articles!