One of the most exiting features in WordPress 5.2 is a simple function that fires an action. But this minor function has a huge impact on theme and plugin developers that inject code to WordPress themes.
When WordPress developers want to insert Google Analytics script, Facebook Pixel or any other script to the page frontend, they hook either to the
wp_head or the
wp_footer actions to inject the code. Those two super useful action hooks allow developers to inject custom code using a simple syntax:
wp_head() function introduced in WordPress 1.2.0, it fires the
wp_head action that adds data to the
<head> tag. The
wp_footer() function introduced in WordPress 1.5.1, it fires the
wp_footer action that adds data to the end of the
The problem is that up until now we couldn’t inject scripts or styles at the beginning of the
<body> tag. This was a major drawback in WordPress core as marketers need to register various tracking codes at the beginning of the body tag before any content is displayed.
New Theme Structure
WordPress 5.2 introduces the
wp_body_open() function which fires the
wp_body_open action. All the default themes use them in their
As of WordPress 5.2, the new theme structure should look like that:
If you are a theme developer you should add the newly introduces function to your theme.
Note that some themes have multiple
<body> tags, depending on their structure. Those kind of themes should add the
wp_body_open() next to the
There are two issues that theme developers may experience and how to overcome them:
Old WordPress Version
If a theme developer will add the
wp_body_open() function to the
header.php it will work as expected. But if the site is using an older WordPress version that doesn’t have the
wp_body_open() function, it will cause a fatal error. You can’t call a function that doesn’t exist.
The simplest solution is to upgrade the theme’s Minimum Required WordPress Version to 5.2, but this won’t provide any backwards compatibility for older version.
To support the new function and action in WordPress versions prior to 5.2, you should register the new function by yourself in the theme’s
Custom Theme Hooks
Many themes use their own custom actions at the beginning of body tag. They should consider migrating to the core
For backwards compatibility, when injecting custom code, theme developers can use conditional logic to hook to the right action
The future of WordPress is bright. More and more new features are introduced and make the development process easier. If you know of a new function, filter or an action that theme developers should know about, please share in the comments bellow.