{"id":978,"date":"2011-06-12T00:02:52","date_gmt":"2011-06-11T16:02:52","guid":{"rendered":"http:\/\/wp.jiinjoo.com\/?p=978"},"modified":"2011-06-12T00:02:52","modified_gmt":"2011-06-11T16:02:52","slug":"looping-in-the-head-wordpress","status":"publish","type":"post","link":"https:\/\/wp.jiinjoo.com\/?p=978","title":{"rendered":"Looping in the Head (wordpress)"},"content":{"rendered":"<p>Just a learning to share with wordpress developers, and to seek some architectural comments.<\/p>\n<p>So today I was doing what every other wordpress person out there has been doing &#8211; 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.<\/p>\n<div style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/developers.facebook.com\/images\/devsite\/open-graph.png\" alt=\"Like!\" border=\"0\" \/><\/div>\n<p>I was using this <a href=\"http:\/\/wordpress.org\/extend\/plugins\/like\/\" target=\"_blank\">plugin called Like<\/a>. As there are a gazillion others, I didn&#8217;t really care if it was actually the best one. Laziness is a virtue sometimes.<\/p>\n<p>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 <b>og:description<\/b> was not showing up.<\/p>\n<p>So I studied the code, and long story short, <a href=\"http:\/\/profiles.wordpress.org\/users\/bottomlessinc\/\" target=\"_blank\">the author<\/a> called the function <i>get_the_excerpt()<\/i> from his header hook, but that function is designed to work only in The Loop.<\/p>\n<p>If you&#8217;re a WordPress developer in any way, you know <a href=\"http:\/\/codex.wordpress.org\/The_Loop\" target=\"_blank\">The Loop<\/a> (no, not the American sitcom&#8230;). The Loop is a rather tyrannical construct &#8211; it has to exist somewhere in the body, and will only populate certain data structures and enable key functions after you&#8217;re &#8220;in the loop&#8221;.<\/p>\n<p><b>Model, then View<\/b><br \/>\nI mean, the <a href=\"http:\/\/ogp.me\/\" target=\"_blank\">Open Graph API<\/a> is pretty cool, mostly because <a href=\"https:\/\/developers.facebook.com\/docs\/opengraph\/\" target=\"_blank\">Facebook adopted it<\/a>. It also works with the framework we&#8217;re all familiar with &#8211; manifesting itself as a &lt;meta&gt; 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&#8217;t change.<\/p>\n<p>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:<\/p>\n<p><code><br \/>\nif (have_posts()):while(have_posts()):the_post();endwhile;endif;?><br \/>\n<\/code><\/p>\n<p>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 &#8211; especially when the global <i>$post<\/i> variable already has the data, it&#8217;s just that normal wordpress functions to extract information and run through all the hooks and data cleansing malfunctions without <i>the_post()<\/i> being executed first.<\/p>\n<p><b>Preparing a useful model right from the start<\/b><br \/>\nSo why can&#8217;t the entire environment be setup first? If there&#8217;s looping required, let it be just an iteration across the data structure that&#8217;s created. It&#8217;s not performance, because all the necessary DB interactions has already happened. <\/p>\n<p>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. <\/p>\n<p>And for situations where there&#8217;s more than one piece of information available (such as non-single post\/page), don&#8217;t assume the end developer is an idiot &#8211; 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&#8217;s consistent.<\/p>\n<p>I hope future versions of wordpress can eliminate this constrain of having useful functions hardcoded for The Loop.<\/p>\n<p>Am I missing something here?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a learning to share with wordpress developers, and to seek some architectural comments. So today I was&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-978","post","type-post","status-publish","format-standard","hentry","category-technology","content-wrap"],"_links":{"self":[{"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/posts\/978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=978"}],"version-history":[{"count":1,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions\/979"}],"wp:attachment":[{"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jiinjoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}