Looping in the Head (wordpress)

Just a learning to share with wordpress developers, and to seek some architectural comments.

So today I was doing what every other wordpress person out there has been doing – leveling up my page by providing better information when people take my links and post into facebook. You know, putting that Like button in there.

Like!

I was using this plugin called Like. As there are a gazillion others, I didn’t really care if it was actually the best one. Laziness is a virtue sometimes.

Everything was fine except, when I like my page, the standard description, rather than the excerpt of the post, appears on facebook. To jump straight to the problem, basically og:description was not showing up.

So I studied the code, and long story short, the author called the function get_the_excerpt() from his header hook, but that function is designed to work only in The Loop.

If you’re a WordPress developer in any way, you know The Loop (no, not the American sitcom…). The Loop is a rather tyrannical construct – it has to exist somewhere in the body, and will only populate certain data structures and enable key functions after you’re “in the loop”.

Model, then View
I mean, the Open Graph API is pretty cool, mostly because Facebook adopted it. It also works with the framework we’re all familiar with – manifesting itself as a <meta> tag instead of being spread all over the body of the code (because, 1 page, 1 node in the social graph). This is good design that shouldn’t change.

On the other hand WordPress was a templating engine that jumps in and out of the MVC as the page is being constructed, instead of a more disciplined way of digging out every piece of dirt that you ever need on a page before printing (echoing?) the first character out of port 80. When I referred to other implementations, people basically did this in the header prior to its population of the OGP meta attributes:


if (have_posts()):while(have_posts()):the_post();endwhile;endif;?>

WTF? To me, this is a subjugation of the wordpress design principle that was completely ignored, possibly resulting in totally unnecessary setting up and tearing down – especially when the global $post variable already has the data, it’s just that normal wordpress functions to extract information and run through all the hooks and data cleansing malfunctions without the_post() being executed first.

Preparing a useful model right from the start
So why can’t the entire environment be setup first? If there’s looping required, let it be just an iteration across the data structure that’s created. It’s not performance, because all the necessary DB interactions has already happened.

The header vs body is a HTML construct which has its merits, and information should not be assumed to only reside in section or the other.

And for situations where there’s more than one piece of information available (such as non-single post/page), don’t assume the end developer is an idiot – if we can find our way through DOM we can find our way through any data structure you throw at us as long as it’s consistent.

I hope future versions of wordpress can eliminate this constrain of having useful functions hardcoded for The Loop.

Am I missing something here?

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top