Backwards compatibility for WordPress 3.4 headers and backgrounds

As I mentioned in the previous post, the new versions of WordPress custom headers and backgrounds have some great new features, but they require WordPress 3.4. That’s no problem if you’re already steering your custom development towards 3.4 (and you should be), but if you’re building themes for distribution, you can’t be sure that all your users are going to upgrade on the first day or even month.

Michael Fields, Automattic Theme Wrangler and all-around smart theme guy, pointed me toward the latest version of P2, which has a rather nice way of handling the backwards compatibility issue for just this case: (This function is called within theme setup)

function p2_setup_custom_header() {
	$args = array(
		'width'               => 980,
		'height'              => 120,
		'default-image'       => '',
		'default-text-color'  => '3478e3',
		'wp-head-callback'    => 'p2_header_style',
		'admin-head-callback' => 'p2_admin_header_style',
	);

	$args = apply_filters( 'p2_custom_header_args', $args );

	if ( function_exists( 'get_custom_header' ) ) {
		add_theme_support( 'custom-header', $args );
	} else {
		// Compat: Versions of WordPress prior to 3.4.
		define( 'HEADER_TEXTCOLOR',    $args['default-text-color'] );
		define( 'HEADER_IMAGE',        $args['default-image'] );
		define( 'HEADER_IMAGE_WIDTH',  $args['width'] );
		define( 'HEADER_IMAGE_HEIGHT', $args['height'] );
		add_custom_image_header( $args['wp-head-callback'], $args['admin-head-callback'] );
	}
}

Elsewhere in the theme setup function, P2 also uses:

	$args = apply_filters( 'p2_custom_background_args', array( 'default-color' => 'f1f1f1' ) );
	if ( function_exists( 'get_custom_header' ) ) {
		add_theme_support( 'custom-background', $args );
	} else {
		// Compat: Versions of WordPress prior to 3.4.
		define( 'BACKGROUND_COLOR', $args['default-color'] );
		add_custom_background();
	}

That’s taken direct from P2 — you’ll have to make adjustments for your own themes: change the slugs, set up the options you want to use, etc.

I can’t think of any reason you couldn’t wrap both function_exists() checks into one conditional, but I’m inclined to keep them separate — it’s easier to read, easier to override in child themes, and if one extra check is a performance concern, it’s time to add a second hamster to your hamster wheel…

(On the other hand, I’m not a fan of holding on to backward compatibility beyond a couple of core versions back. There are a lot of themes out there still checking if widgets are supported, and at that point it’s just cruft. Be nice to your code.)

Flexible headers in WordPress 3.4 themes

As of WordPress 3.4 Beta1, theme authors can allow custom header images with flexible heights, widths, or both.

In other words, instead of being limited to a fixed size determined by the designer, as you’re already familiar with:

With flexible headers, you can allow users to upload smaller or larger headers without having to make a child theme or (worse) hack your theme, like so:

How to do it:

Image header support in 3.4 no longer uses all the constants that had to be defined in previous versions — HEADER_IMAGE_HEIGHT, HEADER_IMAGE_WIDTH, HEADER_TEXT_COLOR and the like are now gone. Instead, all the header support options are included in a single array, and enabled with a call to add_theme_support() in your theme’s functions.php file.

This is how the array looks in the theme I used for the screenshots above:

// Add support for flexible headers
 $header_args = array(
 'flex-height' => true,
 'height' => 200,
 'flex-width' => true,
 'width' => 950,
 'default-image' => '%s/images/headers/path.jpg',
 'admin-head-callback' => 'mytheme_admin_header_style',
 );

 add_theme_support( 'custom-header', $header_args );

If flex-height or flex-width are not included in the array, height and width will be fixed sizes, replacing the old HEADER_IMAGE_HEIGHT and HEADER_IMAGE_WIDTH. If flex-height and flex-width are included, height and width will be used as suggested dimensions instead.

Don’t forget to remove any references to the old constants in your template files — in my header.php, I simply replaced.

<img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />

with:

<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />

For reference, here’s the full set of options and their default values, as found in /wp-includes/theme.php. Note that the other header image options like text, text color, and randomization are available in the same add_theme_support() call by setting only the options you need:

$defaults = array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => true,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);

A few important notes:

  • Flexible header support has to be built into each theme – it will not work automatically with your pre-existing themes.
  • This functionality requires WordPress 3.4, which is currently in beta. You can grab the latest here (.zip file). Please use it for testing and development, but don’t run it in production unless you know what you’re doing and have some risk tolerance.
  • Design-wise, flexible heights are relatively easy; flexible widths are much trickier to work into a layout while still looking good. Experiment!

https://twitter.com/#!/Malarkey/status/175366107786522624

As a quick followup to the previous licensing post, and in light of the current discussions on WordPress plugin licensing, I’ve decided to use MIT license for packaged code and Public Domain for code samples in posts from now on. None of this invalidates my previous argument, of course — pick a license, understand what it does, and unless you’re developing a whole new license for some excellent reason, don’t make up your own weird terms that downstream projects can’t easily work with.

Against DIY Licensing

Because my FOSS1 work is mostly WordPress these days, my baseline is the GPL. GPL is far from the only license out there, but — and this is the key point here — it’s widely used and (at least more or less) understood.

The GPL comes with a provision that derived works must also be GPL-licensed2, so I spend a certain amount of time hunting down whether some bit of code I want to use can legally be incorporated into such a project. And we do the same thing when reviewing themes: everything that goes in the WordPress.org repository has to be GPL-compatible, so all those nice textures and icons and webfonts that can really make a theme stand out have to be compatible as well.

Note: Compatible.

Not everything in the open-source universe has to be, or should be GPL. But everything that goes into a GPL project has to play well with the GPL.

There are wide swaths of the open-source world for which GPL is too restrictive, and people working in those areas tend to go for permissive licenses like BSD or MIT, or release their work into the public domain outright. And that’s awesome. For some kinds of projects, it’s way more appropriate than a restrictive license. If what you’re releasing is the code equivalent of infrastructure, it will not be useful, and will not spread, with a lot of restrictions on how it can be distributed. Permissive licenses are great for libraries, glue, shims, resets, and all that magic stuff.

Creative Commons — for content, not software — is absolutely one of the more brilliant ideas ever to come along. But remember, there are at least half a dozen such licenses, and they range from totally free, no rights reserved, no restrictions of any kind at all, all the way up to You Can’t Touch This3.  Occasionally people pop up in the theme review mailing list or IRC channel and ask “is a CC license okay”; much more often, people put things out there that just say “CC licensed”, which isn’t properly licensed at all. If anyone’s going to use the stuff, we need to know which rights you’re actually licensing.

Free-as-in-speech fonts finally seem to be standardizing more and more on the SIL Open Font License, no doubt thanks to the runaway success of Google Webfonts; the move to standardization makes things nice and easy. It’s also a nice license: quite permissive and GPL-compatible, and designed for the way fonts are actually built and used.

Notice that the above licenses are really far apart in what they allow, and (free software being chock full o’ free software people as it is) we occasionally have full-scale wars about which one is better. But also notice one similarity: they’re all well-known, widely used, documented. They’re suited to particular purposes: If you want your code used absolutely everywhere without any barriers, use a permissive license; if you want to spread the free software gospel, copyleft. The common licenses are standards, in a way. I know without having to put my project aside for a side trip to the local law library that I can incorporate MIT-licensed code in a GPL project, and also that I can’t do the same if I’m going the other direction. I know that I can package up a theme with a CC-0 photo, but not with one that’s CC-BY-NC-ND.

And ultimately, I don’t care all that much which one you choose — I’ll give you the benefit of the doubt and assume that you chose the path you did for reasons that you knew and fully understood at the time. I just want you to choose a path that allows me to understand how I can and can’t use this thing.

But instead, I keep seeing handmade and non-standard things like4 :

License:
– you can freely use it
– you can freely distribute sourcecode
– you can freely modify it as long as you leave my copyright/author info in source code
– if you developing closesource application, you should add my name at least to “about” page of your web application
– if you create an amazing modification, please contact me… I can publish link to your webpage if you’re interested…
– if you want to use my script in commercial application for earning money, you should make a donation to me first

Or5 this one:

– All themes are free to use as long as you leave the credits links unchanged in footer.
– You cannot claim our themes or modifications of these themes as yours.
– You cannot modify our themes and distribute them on your website.
– You cannot sell our themes for others.
– We does not allow you to use our themes for websites or individuals that participate in warez, hacking, cracking, malicious computer crime or fraud, or any other material or activity that is illegal.
– We does not allow you to use our themes for websites or individuals that participate in porn, gambling, poker, insurance, forex….

And here’s the thing. After seeing untold dozens of such licenses, I don’t even bother to study them and puzzle out if they’re maybe possibly compatible any more. If the developers actually wanted their stuff to be used, they would’ve made it so, and saved themselves a lot of time thinking about exactly which page a linkback needs to be on, or what list of industries they find too icky. Having actually read a bunch of these, they’re never okay to use in projects or put in the repository, and in the current climate, there is always another library or another template out there. Always.

(And on a personal note, for doG’s sake, please don’t call your homegrown terms a EULA. That does not impress or inspire. It just makes me think of 50-page-long click-through legalese from Sony or iTunes.)

  1. Fuck it. I’m saying “open source” from now on. Because it’s English words. Feel free to substitute your terminology of choice.
  2. Leaving aside all connotations of good and evil, this is exactly the replication mechanism of a virus, or more precisely a meme (using the original definition). Cool!
  3. AKA No-Derivatives.
  4. Hey, pay me and I might give you a linkback!
  5. Don’t use my stuff for pr0n!

Plugin: Blogify Posts Menu

I hate “Posts”. Oh, I don’t mean that I hate posts, and in fact I’m kind of in love with making more of them now that I’m finally getting back in the habit.

No, I mean I hate the thing in the WordPress menu that says “Posts”, because when I’m looking at the menu for what to click, I’m thinking, “I want the blog”. And because I’ve seen about a bajillion new users get really, painfully stuck on Posts and Pages and What’s the difference, and How do I know which one to use, and even after explaining, still just want to know, “but how do I post to my blog?”

When it happens that consistently, it isn’t a problem with the users. They’re simply telling us that we need a better label.

This plugin changes the label. No muss, no fuss, no configuration needed. Scratch your own itch, or add it to your standard bag of tricks when building sites for other people. Grab the plugin and more details at the WordPress repository or on the plugin page.

WPTRT Review-a-thon tomorrow

The Review Team has been playing around with ideas to get through our backlog of themes to be reviewed, so I’ve proposed an IRC meetup in #wordpress-themes on Freenode, tomorrow at 1700 UTC / noon US Eastern.

The model I have in mind is http://codex.wordpress.org/WordPress_Bug_Hunts

In particular, I want to invite any new or wanna-be reviewers to join in; this would be a great chance to get started with reviewing, get your test environment set up and work through your first few reviews with help on hand to understand how the guidelines work.

We have just a few ground rules:

  • This isn’t a meeting with an agenda: it’s a group work day. Drop in for as long as you’re available, and get as many reviews done as you can during that time.
  • The goal is to crunch through as much of the queue as possible while helping and learning from our peers.
  • All reviewers, however new or experienced, can bring questions about how to handle particular issues. Absolutely no penalties for admitting you’re stuck ;)
  • Themers, you’re welcome to join in the discussion, try your hand at reviewing, or just lurk and learn the process. However, please don’t use this time to ask about when your own theme will be done — we’re taking everything in queue order, and we’d most likely just encourage you to sign on to review a few themes.
  • For new or potential reviewers, more experienced people will try to help out as much as we can with getting started, working with trac, setting up your test environment and the like.

 

So, that happened

I’ve never seen a day like yesterday online. Yeah, Wikipedia being out got most of the focus, and a lot of that was jokes at the expense of people who were kind of terminally clueless. People will forget about it over time, or take away the wrong message (already, I heard a BBC commenter say, “So, this is a blow against intellectual property?”… no, just against the use of it as an excuse to stifle completely unrelated areas.) It’ll be easy to forget the feeling of watching history change in front of our eyes, as the day went on. So consider:

Until the internet spoke up, SOPA and PIPA were considered to be a sure thing, with overwhelming majorities in both houses. Even worse, they were considered to be sure to pass because nobody would even bother to learn what was in them. 

According to SOPAstrike.org, over 75,000 sites, large and small, took part in the protest. It was more than that, really: that’s only the people who added their names to the list.

Over 7 million people signed Google’s petition. It’s still up, if you didn’t get to it yesterday.

Thousands of people showed up in person in New York for the NY Tech Meetup’s protest at their Senators’ offices.

Dozens of legislators announced their opposition to the two bills. Some of them were previously unaligned, but the list includes several who were previously in favor, even co-sponsors of the bills.

We — the internet, the meme fields, the digital wild west — did a thing. Don’t forget it. But above, don’t stop until it’s done:

ACTION: If you couldn’t get through to your congress members because their phone and fax lines were slammed and their websites went down, contact them now. Even if you did get through, contact them again.

ACTION: VOTE.