Standardizing classes, hooks, and templates with hybrid_get_context()
One of the big things I'm working on in 0.7 is creating the
hybrid_get_context() function. This function is for having a standard way of understanding the context (what page we're viewing). It doesn't just give you one context though, it returns an array of contexts that can be used to determine various things about the page. For example, a single post might have a context that looks like this:
/* The type (singular, archive) page we're viewing. */
/* What post type we're viewing. */
/* The ID of the object we're viewing. */
Shaping the system
Well, we've had a contextual
<body> class for some time now, even before this was added as a standard in WordPress. But, if you ask me, what's in WordPress is not as clear and organized as it should be. It still holds on to old methods that should be changed to accommodate a more organized template hierarchy. One of my goals I talked about in my original post called Why I created a WordPress theme framework talked about how "WordPress is flawed":
When I say WordPress is flawed, I don’t mean that the system is bad. I mean this opens opportunities for theme and plugin developers to shape the sytem into something better than what it already is.
I believe I have an opportunity here to shape the system into something better.
What does all this mean?
hybrid_get_context() returns an array of contexts based on the current page. These contexts can be used in any number of ways. Specifically, there are three major areas that could benefit from a single contextual function:
- Contextual hooks.
- Template hierarchy (though I'm likely not going to implement this soon).
So, let's say you wanted to change the look of page with a custom background image. You'd have three classes to choose from:
<body class="singular singular-page singular-page-100">
singular would refer to all single views (post, page, attachment, custom post types, etc.).
singular-page would refer to all Pages. And,
singular-page-100 would specifically refer to a page with the ID of 100. We can already do this, but it's about standardizing the names and streamlining the process.
What happens when you want to insert an ad in the header given a page's context? Currently, you have to write the "context" stuff yourself; you have to figure out the logic behind it all. With the new system, we'd have these hooks:
This does a few things:
- Works out all the logic behind the scenes for you.
- Allows you to hook anything, anywhere according to a page's context.
- Gives you a cascading flow of hooks (from general to specific), so you can add actions or remove actions based on context.
Faster PHP processing
The great thing about
hybrid_get_context() is that it only needs to run one time on a page. It sets a global variable of
$hybrid_context that will be used if the function is called again and returns it instead of running through all the code. While it'll probably not cut back in a noticeable way, anytime you run less code, it's better.
Standards and streamlining the code
As a developer, you're always looking for ways to make your code more efficient. Reusing code is a cornerstone of good development practice. Having one function that figures out all the logic just makes sense from a development standpoint.
In order to do this, the standards must change because the way WordPress handles this does not lend itself well to things like custom post types and custom taxonomies. Nor does it account for more than a handful of common contexts. This is because of the great Sandbox theme. Whoever added the
<body> class into WP just took the code from the theme but didn't try to make it better.
What are the available contexts?
I'm still working out the kinks with the final naming conventions. Feel free to offer suggestions and alternatives to what's below.
The context shown in hierarchical view:
home (front page of the site)
blog (posts page, which can sometimes be the front page)