Idea: Static Templates for WordPress

I was helping out a friend a few days with their fairly complicated WordPress website. Complicated because it had over 40 different page templates, rendered exclusively by PHP.

Each page template had something special about it, like a form and form handler, a JavaScript calculator, a list of registered users, the current user profile and more. Page Templates worked pretty well, and the other “simple” options were shortcodes (bleh!) or page-{slug}.php templates.

Anyway, when this friend of mine was porting the site over from his development environment to production, he had to recreate those 40 pages, make sure their slugs matched, and assign the appropriate page template from that drop-down list of over 40 times. What a nightmare!

Static Templates

I thought there should be an easier way. One that would work similar to page templates, but without that extra database overhead, without the UI and everything else. Obviously this would render a WordPress theme completely useless outside of the original context, but so do 40 page templates.

I drafted up a plugin, a proof of concept, and called it Static Templates. The idea is for a theme to have a static-templates folder, with .php templates named after certain slugs. For example:

/about/           => /themes/foo/static-templates/about.php
/about/contacts/  => /themes/foo/static-templates/about-contacts.php
/foo/bar/baz/     => /themes/foo/static-templates/foo-bar-baz.php

So when /about/ is requested, you don’t need to worry about creating a page in WordPress, creating a new template and assigning it to your page. All you have to worry about is your /static-templates/about.php file.

Obviously if a page with that slug exists in the WordPress database, it’ll be loaded instead, so static templates have a lower priority, and there’s still that little overhead of trying to fetch a non-existent page from the database before a static template is invoked. It also does not (yet) distinguish between /foo/bar/ and /foo-bar/ requests.

Anyway, as I mentioned this is just an idea, and I would absolutely love to hear your thoughts about it.

Did the Toolbar (Admin Bar) Render edit_post_link Useless?

I asked this on Twitter yesterday which raised quite some interesting replies from theme developers, the .org themes review crew and other folks.

For those of you who don’t know, the edit_post_link function is widely used in WordPress themes, to create an “Edit this post” link as a shortcut to the post edit screen. This was convenient I agree, except that the link was always in different places for different themes. Some put it near the tags and categories, others squeeze it in at the end of the post, or even the bottom of the page, so it’s not always easy to spot one, after activating a new theme.

The WordPress Toolbar (the former admin bar), however, changed that story. I started using my blog’s user facing side more that the admin panel. I use the “New” menu very heavily and when I need to edit a post or page, I can easily find the Edit Post button in any theme.

Edit Post in Admin Toolbar

Hence my question — do we still need the edit_post_link function? If there was an option to retire it, I’d vote for that. If the admin toolbar is turned off, well tough luck, but as Emil mentioned, we can always use the is_admin_bar_showing function and fall back to edit_post_link if needed.

However I think that’s just too much. You can turn the Toolbar off for your visitors, you can turn it off for subscribers, but please don’t turn it off for authors and editors! It’s almost like turning off the Media Library and saying “you know what, why don’t you FTP your images instead” ;) I believe that the admin toolbar is an essential WordPress experience, and shouldn’t be turned off.

The other, more valid reason for keeping edit_post_link is that we can use it in a list of posts, so to edit a post you don’t have to actually visit it first, but can do it directly from the list. I agree, especially if an extra page load takes 10 seconds. But how is that different from your Edit Posts screen in the Dashboard, right?

In any case, I can often hit the permalink and then Edit Page in the admin bar, faster than I can spot the edit link generated by edit_post_link, especially when I’m running Multisite with a bunch of blogs and a bunch of different themes. Because it’s in the same place for every theme, and because it’s always there. I’m used to it.

Your turn to speak! Do you still use edit_post_link when developing your themes? Do you use it when browsing around your theme? Do you disable the admin bar in any of your projects and why? Finally, do you think it’s time to deprecate the edit_post_link function? Thanks for sharing your thoughts and comments, and don’t forget to subscribe!

Get an Expired Transient in WordPress: Good Idea or Crazy Talk?

I wonder if there’s an easy way to get an expired transient in WordPress? Now, for this to make sense I guess I should provide a little more context, so here it goes :)

The Transients API is an extremely easy way to cache parts of your WordPress code, that may be CPU/memory intensive, or rely on a third-party server and so on. A great example is grabbing a tweet from Twitter and caching it for a few minutes, so that we don’t query the Twitter API on every page load. So in theory:

if ( false === ( $tweet = get_transient( 'my_latest_tweet' ) ) ) {
    $tweet = get_my_latest_tweet(); // Queries the Twitter API
    set_transient( 'my_latest_tweet', $tweet, 60*60 );
}

Is a great way to cache my latest tweet for an hour. Perfect, but there’s a problem. The code above will work well for 60 minutes. When the transient expires, it will have to spend time again to fetch the tweet, thus impacting your page load time. Once every 60 minutes.

Suppose Twitter is being slow today and taking 2 seconds to respond back, and suppose I want to cache the tweet for 5 minutes, not an hour. This means that every 5 minutes, somebody will have to wait two extra seconds for my page to load. Suppose I’m querying the Flickr API, the Facebook API and fetching a few RSS feeds too, that will all add up to the page load time, to that unlucky person who visited my site, when the cache was expired. Bummer!

Is there a solution? I don’t know, but I can think of one — always serve cached data, even if it’s expired. That way visitors will never have to wait extra when you’re speaking to third party servers and APIs. I can think of a way to accomplish this by fetching the new data in a different request, transparent to the user.

Like an asynchronous request with jQuery (aka AJAX) to a special action that would revalidate expired cache. Crazy talk? Here’s the scenario:

  1. You visit your page when caches are empty, you don’t see the tweet.
  2. An async jQuery request is fired to the server which grabs the latest tweet and caches it, this happens behind your back.
  3. You refresh the page and you see the tweet from cache. Yey!
  4. Ten minutes have passed, cache is expired, but not trashed. You visit your page, you see the old tweet from (expired) cache.
  5. An async request in the back fetches a new tweet from Twitter and replaces the one expired.
  6. You refresh the page and you see the (new) tweet from cache. Yey!

So in steps 1, 3, 4 and 6 you’re always serving what’s in cache, whether it expired or not, even if it’s empty (first step) so you’re serving as fast as possible. Steps 2 and 5 happen behind your back, they don’t impact page load time, and they never return anything back. You won’t see them happening unless you’re looking at your Netwok tab in Chrome’s developer tools or Firebug.

Now, suppose Twitter is down. The visible steps are not impacted, because they return stored data, they don’t have to fetch it from Twitter. The hidden steps will fail, but your visitors will never know, since they’ll be still seeing the old tweet fetched some time earlier, right?

This can easily be done with the Options API but hey, transients are already a great tool for caching. Wouldn’t it be even easier if transients never actually expired? And a function to fetch such an expired transient, get_expired_transient perhaps? :)

Okay, that’s just off the top of my head, it’s quite late so, sorry if I’m totally on the wrong track. Let me know if there’s an easier solution, or maybe a caching plugin that already does this. Object caching can benefit too by the way. Share your thoughts in the comments section below!

Hey WordPress, How About a WP_Plugin Class?

Okay January is my month of ideas :) Let’s talk about plugins for a moment, shall we? Actions and filters are no secret to WordPress developers, right? Say, how many times do you type something like this in your plugins or theme files:

class Some_Plugin {
    function __construct() {
        add_action( 'admin_init', array( $this, 'admin_init' ) );
    }

    function admin_init() {
        // Whatever
    }
}

If you don’t, then you can stop reading now :) I’m addressing the naming technique here, the fact that admin_init is both an action tag, and a method that you assign to that action tag. So if I’m naming all my methods after actions and filters tags in WordPress, isn’t there a way to cut down the burden of having to add_action and add_filter all the time?

I’ve been thinking of an approach that could solve this problem in both, an easy to understand way, and a compatible way. Perhaps we’re lacking a WP_Plugin class in WordPress that would take care of this (along with a bunch of other things) for us, so that we can focus on writing our plugin, and not spend time matching tags to methods.

Here’s a concept that first popped into my mind, experimental and buggy, but something to get our heads thinking about it. If your class extends the WP_Plugin class, and your method’s name starts with a double-underscore prefix (or any other convention we may choose), and matches an existing action or filter, why not run the action or filter automatically without having to add_action or add_filter?

class Some_Plugin extends WP_Plugin {
    function __admin_init() {
        // Will run during admin_init
    }

    function __the_content( $content ) {
        // Will filter the_content
        return $content;
    }
}

Again, it’s just a thought and a lot of decisions should be made before implementing something like this. I went ahead and drafted a plugin file which works well to some extent. You can change the method name convention on line 18.

So my questions to you, dear reader are: is will this make things easier for the WordPress developers? Will this mess things up? What do you think of the prefix convention? How would you propose to handle the priority dilemma? What else could go into a WP_Plugin class?

Update! It seems like Reflection is a better approach at what I’m trying to do, and separating actions from filters logic too. Thanks so much for the heads up Ryan McCue, Kaiser and Daniel Dvorkin. Here’s Ryan’s improvement over my first Gist.

Thanks for stopping by and subscribing!

Dear WordPress, I Want to "Drop Files Here" in the Visual Editor!

Wouldn’t in be great if we could drag and drop to upload files directly into the visual editor? WordPress 3.3 very well handles drag and drop uploads into the Add Media modal box, in a special area that says “drop files here”:

WordPress 3.3 Drop Files Here

And thank you so much for that, WordPress, but I’m tired of constantly having to open the media manager to insert an image into my post or page. Having the visual editor carry out the “drop files here” role would be awesome, it would save at least one click every time I need to insert an image.

Two clicks if we can get it to upload and insert the image into the editor without even showing the Add Media screen! But that might be a little more complicated because of positioning and sizing. A simple upload is where we should start.

Continue reading

Every Keyboard Needs A Share Button

I thought of this idea a few days ago and tweeted it out loud too. Today I’m taking it forward and introducing it to you, my dear readers — a brand new keyboard with a share key, conceptually of course.

Social media has exploded in the past few years, and what is social media without the ability to share stuff? Blog posts, videos, photos, whatever. Thousands of plugins, widgets and bookmarklets have been developed, which allow you to share the web page you’re viewing, a YouTube video or a screenshot of your desktop. Good, but it’s time to take this to the next level, directly on the keyboard. Multimedia keyboards have been around for over a decade and heck, nobody uses them except that popular “mute” button!

You don’t mute your speakers every day though, but what you do everyday is share. You share lot’s and lot’s of different content with your friends, family, co-workers and the rest of the world. You tweet, you digg, you stumble and you reddit, you facebook, you quora and you vkontakte for Christ’s sake ;) Wouldn’t it be wonderful if there was a key for that? I mean there are 12 freakin’ function keys (F1-F12) — those are pretty useless! And the scroll lock? What about a certain “Alt Gr”? I have never, ever pressed that! Nobody even knows what “Gr” is! Here:

Share Button

See? That was easy! Let’s call it the Social Keyboard! And there’s more!

What Happens When the Share Key is Pressed?

Good question, glad you asked! What happens when you press, say Ctrl+F? That’s right — depends on what you’re currently doing, i.e. which application is active. You’ll get the search bar in Google Chrome and other browsers; you’ll get a search dialog in a word processing application; you’ll get the file search in your file explorer; Google Docs will cover up with it’s own search box; and you’ll probably get nothing if you’re running Minesweeper or whatever.

So the action is based on the application, where some applications might delegate the action. For instance, if you’re running Photoshop and hit the Share button, your current image will be saved and uploaded to Dribbble, Forrst or whatever you pick. If you’re browsing your hard drive, pick a photo and hit Share, the file will be uploaded to Twitpic or Flickr. If you’re listening to a song in your favorite player and hit Share, it’ll create an entry on your Blip.fm or Twisten.fm account. What about the browser? Well, this needs a separate paragraph.

If you hit Share in your browser, your browser will either delegate the action to the current web page that can intercept the key press using JavaScript, or if that’s unavailable, it’ll handle the sharing action itself — publish a link to the current page to Twitter, Facebook, Digg or whatever. Voila! Below is a set of screenshots I prepared to illustrate all that I wrote above.

Conclusion

There is a browser for social media (I’m talking about RockMelt), so why can’t there be a social keyboard? I’m tired of all these bookmarklets, widgets and apps that help me (or at least try to) share stuff online. A simple key on my keyboard will make life so much easier!

I know there’s quite a lot of work involved in developing such a product, drivers and everything, and then get software and web developers to adopt it, but hey, you did it with the multimedia keyboards! I know they turned out to be pretty useless but please, give it another shot. Sony, Logitech, Genius, Microsoft, Defender, or perhaps Apple? Who’ll be the first?

Thank you for reading. Bye bye!