What The File 1.6, WP admin bar hooking changes

What The File 1.6.0 released

It took some time, but a WordPress update some time ago partially broke What The File. With WordPress changing the action where the admin bar is rendered, template part support for What The File was broken. With this feature being added April 11th, 2013, I’d say we had a good run.

Admin bar rendering

In code, What The File, is a pretty simple plugin. It listens to the template and its parts that are included, remembers this and when the whole page is rendered it adds these to the admin bar. In WordPress core, the admin bar was added in the wp_footer action (with priority 1000), so this was perfect for our use case. At this point, every template part was included, so we simply looped through our array and add them to the admin bar.

Some time ago, WordPress introduced the wp_body_open action. This action should be called by themes after opening the <body> tag. In WordPress 5.4 the wp_admin_bar_render function was called from the new wp_body_open action on top of from the wp_footer action. Yes, on top of, as a fall-back system. The wp_admin_bar_render function checks if the admin bar is already rendered. If it is, the function does nothing.

It took some time for this to become an issue for What The File, because at the start most themes didn’t support the wp_body_open action. As more and more themes did, template part support stopped working for more themes.

Injecting template parts after page load

So while we’re still correctly logging the template parts included, the admin bar is now already added at the start of the <body> tag. Before any template parts are loaded.

Since wp_body_open is a feature WordPress wants themes to use, removing support for this action (to force a fallback to wp_footer) is not desired. So I needed a way to change the admin bar after it is rendered.

I ended up with a JavaScript solution. Via the wp_footer action, the plugin is now printing a JSON-encoded array of template parts in your HTML. The plugin uses this array on page-load to “inject” these via DOM manipulation into your admin bar. This whole process is only done when an admin bar is shown.

The JavaScript is written in “vanilla” JS, so no additional library like jQuery has to be included. This also saves the headache of jQuery version / bind conflicts caused by themes that come with their own versions.

Update now available in your dashboard

I took this time to improve some other minor, technical, things in the plugin as well. With the help of suggestions via the WordPress forums and GitHub PR’s, a lot of small improvements were also done. You can view the full changelog here, and download the plugin via the WordPress repository or via your WordPress dashboard.

Are template parts working again for you? Is there a feature you’d love to have added to the plugin? Please tell me in a comment below, I’d love to hear your thoughts.

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

Leave a Reply

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