SOPA Blackout

I’m lazy and I don’t want to write a plugin, so I asked on Twitter if someone had one to black out a site for SOPA on the 18th of January. If you don’t get why this is a thing, please read WordPress.org’s post on SOPA. A lot of sites are going black from 8am to 8pm. I am, and I to didn’t want to use a plugin.

My solution for this involves two files and is all thanks to Pete!

To do this is really simple. Go in and make a file called .maintenance in the top level of your WP install. For most people, it’s the same folder as your .htaccess and wp-config.php files.

Now edit that file and put in the following content:

<?php
 
// Let's make sure you're not an admin/logged in!
 
function is_user_logged_in() {
    $loggedin = false;
    foreach ( (array) $_COOKIE as $cookie => $value ) {
        if ( stristr($cookie, 'wordpress_logged_in_') )
            $loggedin = true;
    }
    return $loggedin;
}
 
if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin') && ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php') && ! is_user_logged_in() )
 
// If it's after 8:00:00 on the 18th, we're going to show the blackout page
 
if ( time() >= mktime(8,0,0,1,18,2012) ) {
	// When we hit 8pm (20), we're back.
	$upgrading = mktime(20,0,0,1,18,2012);
}
?>

What does this do? It’s pretty simple. First it checks to see if you’re logged in and, if so, lets you get where you need to go. If you’re not logged in, though, it checks the date and if it’s after 8am on the 18th, you get a maintenance page. After 8pm on the 18th, it swings back. 1 Pretty cool right? And the 8am to 8pm is based on your server’s time, so it’s all local. It’s also pretty easy to see how you change that for your own time. I would say pick your busiest time to go black.

But if you want it to look pretty, you need to do a little extra. 2 Make a file called maintenance.php and put it in your wp-content folder. Here’s an example:

<?php
/* Tell search engines that the site is temporarily unavilable */
$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) $protocol = 'HTTP/1.0';
header( "$protocol 503 Service Unavailable", true, 503 );
header( 'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <title>BLACKED OUT</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
        body {
                background: black;
                text-align:center;
                color: #eee;
                font-family: Helvetica;
                font-size: 2.2em;
                }
        </style>
</head>
<body>

<div style="width: 960px; margin: 0 auto;">
<h2>This website is..</h2>

<h1>BLACKED OUT!</h1>

<p>In protest of pending US legislation, which threatens the freedoms of websites like this one and the freedoms of the people who use them, this website is offline on January 18th, 2012 from 8am to 8pm EST.</p>

<p>Please <a href="http://americancensorship.org/">help protect our free speech</a> against the corporate and political interests which seek to take them away!</p>

<p>(And don't worry, we'll be back in business tomorrow!)</p>
</div>

<?php
die();
?>

Which means your site will look like this on January 18th. You’ll notice I made some tweaks on mine. This is a default feature of WordPress, by the way. You can design your own maintenance mode page just with this one file, so y’know, party time.

Now there’s only one catch. If you update WP via the automatic updater between now and then, you lose the .maintenance file. I don’t have a fix for that, but if you do, please comment!

And yes, my sites are going dark on the 18th.

ETA: If you have a Multisite and only want SOME sites dark, use Customize the Suspended blog page from WordPress Must-Use Tutorials. Suspend the sites and boom goes the dynamite.

ETA 2: If you’re using MediaWiki (like I am on another site) I came up with this idea to go dark for SOPA. Its just a normal extension that, between those hours, redirects to the same page I use for WordPress. You could have it point to any page, of course.

<?php

if ( !defined( 'MEDIAWIKI' ) ) die();

if ( time() >= mktime(8,0,0,1,18,2012)  && time() <= mktime(20,0,0,1,18,2012) ) {
     
        include('/location-of/wp-content/maintenance.php');
    exit();
}
?>

That code, sans the Mediawiki line, will also work as an extension for ZenPhoto, so pass it on.

Notes:

  1. All this code, except for the date stuff which I wrote, is from Matt (Sivel):
    WordPress Maintenance Mode Without a Plugin and WordPress Maintenance Mode Without a Plugin Part 3/
  2. Code from WordPress Maintenance Mode Without a Plugin Part 2 and text from Blaccupy
StudioPress Theme of the Month

Comments

  1. And/or you might wanna get a Stop SOPA Ribbon :)

    • I thought about that. Given that the 18th is going to be a HUGE day on one of my sites, going totally dark for 8 hours will freak them the hell out as well as bring awareness where it’s needed. Its a fan site. With SOPA, only one comment, infringing on copy, would bring everything down and send me off to court, if not jail. They need to understand.

  2. As there are plenty of non American people/sites who still would like to help the cause, it would have been nice to make the script in such a way that your Chicago Standard Time (whatever that time is in comparison to Greenwich Mean Time) or any US-based timezone in comparison to the timezone of the site that wants to support the initiative comes out the same.

    I am located in GMT+8, so your 8am to 8pm would probably be something like my 10pm-10am+1, hardly having any effect, especially not the desired…

    Just sth to keep in mind next time you’re “organizing” sth like this…

    • I’m not ‘organizing’ anything :) I’m helping folks along.

      This script actually goes 8am to 8pm on your SERVER time. My server? Set to CST. Which is why my sample text says that. If you put the . maintenance file on your site, it would be 8am to 8pm your server’s time. I strongly urge folks to change the time based on their server, and when they have the most traffic.

  3. For other apps I use, like MediaWiki and ZenPhoto, I wrote a plugin: http://pastebin.com/WLa8iJh0

    Remove the mediawiki line and it’s magically usable on just about anything. Will I lose Google Juice for the 8 hours? Maybe, but it that is your real concern, then I don’t want to hear about it.

  4. There’s some plugins for that:
    http://wordpress.org/extend/plugins/

    ;)

  5. VoodooPress will be joining you in your blackness!!

Trackbacks

Half-Elf? Try Half OFF WordPress ebooks!