Justin Tadlock · July 20, 2020 · 20 Comments
Theme developers can finally rejoice. For the first time, it is now possible to pass data to templates via the various core template-loading functions. Enrico Sorcinelli announced the change on the Make Core blog this past Friday.
The feature was originally proposed by Scott Kingsley Clark in 2012. Over the years, the ticket has received a dozen patches. It has survived a closure and arguments over why the feature should not go into core. Sorcinelli was a primary driver that kept the ticket alive for the last few years.
WordPress developers have been cleaning up some old-but-useful feature requests for the 5.5 release cycle. A couple of weeks ago, an 11-year-old ticket to allow users to update themes and plugins via a ZIP file made the cut. Core developers even closed a 9-year-old ticket related to an Internet Explorer 6 hack — progress. However, for theme authors, one of the most important additions is control over passing data from one template to another.
Typically, in PHP, variables can be passed from file to file because they remain in the same scope. However, that is not the case if the inclusion of the file is taken out of that scope by including the file from inside of a function. The scope is then limited to the function. That is how the template system works in WordPress. This is not necessarily a bad thing. However, it has meant that theme developers have had no built-in method of passing data from one template to the next.
Imagine creating a variable in one template but needing to access that same variable in a sub-template. There is no shortage of methods to accomplish this, but many are inelegant.
“For years, theme developers wishing to pass data to template files have had to use less than ideal workarounds,” wrote Sorcinelli in the announcement. The worst solutions typically involved creating a global variable. Others created custom template-loading functions on top of WordPress’s existing system. The problem with all methods, regardless of which was ideal, was that no standard existed. Each theme would need to build its own solution, and plugins that touched the front end would often have a competing solution.
All of the WordPress template-loading functions now support an additional parameter of $args, which allows theme authors to pass along an associative array of data to the loaded template. The functions that support this new parameter are:
Any hooks associated with the functions also pass along the data.
The get_search_form() function has supported a similar parameter since WordPress 5.2. In practice, it should work mostly the same, but the function has a couple of default values it sets.
An additional benefit of having a standard method of passing data to templates is that the feature can be built upon in the future. For example, WordPress could eventually offer a hook for filtering the data, which could come in handy with child themes.
The WordPress template system still lacks the robustness of more modern frameworks, but this simple change will allow for a variety of applications.
One question remains: is the arrival of this feature too late? With WordPress on track to revamp the entire theme system to integrate with the upcoming full-site editing feature, will this feature be useful for only the next few months?
Even if most theme developers don’t immediately jump on board the block-based themes bandwagon for another year, the feature could come in handy until they do. Perhaps it will also have some usefulness beyond the current theming paradigm.
Developers still do not have a clear picture of what theming with blocks will look like in the next few years. There may be situations where passing dynamic data is still necessary in the next system. Even if not, it will likely be a long while before there is mass adoption of block-based themes from the existing theme development community. In the meantime, many will be able to drop in-house solutions and use standard functions.
Posted in Development, News