Using get_template_part() for WordPress 5.5 and above

You can now pass an array within the function arguments of get_template_part() meaning that you can pass data into the specified template. This can be as simple as a string variable, or as complicated as data can be (array of values, etc.)!

This means that you don’t have to use
include( locate_template( ‘content-page.php’, false, false ) );
More info can be found within the function reference on the WordPress developer docs.

No more locate_template required

This proves very useful in readability and making it easier for WordPress developers to maintain their code, because what makes the get_template_part() function different from the PHP core function locate_template() is that the get_template_part() function handles this for you.

If you have a look at the code taken from the WordPress 5.5 core file wp-includes/general-template.php

Link to the Github WordPress repo for 5.5 get_template_part()[pastacode lang=”php” manual=”function%20get_template_part(%20%24slug%2C%20%24name%20%3D%20null%2C%20%24args%20%3D%20array()%20)%20%7B%0A%09%2F**%0A%09%20*%20Fires%20before%20the%20specified%20template%20part%20file%20is%20loaded.%0A%09%20*%0A%09%20*%20The%20dynamic%20portion%20of%20the%20hook%20name%2C%20%60%24slug%60%2C%20refers%20to%20the%20slug%20name%0A%09%20*%20for%20the%20generic%20template%20part.%0A%09%20*%0A%09%20*%20%40since%203.0.0%0A%09%20*%20%40since%205.5.0%20The%20%60%24args%60%20parameter%20was%20added.%0A%09%20*%0A%09%20*%20%40param%20string%20%20%20%20%20%20%24slug%20The%20slug%20name%20for%20the%20generic%20template.%0A%09%20*%20%40param%20string%7Cnull%20%24name%20The%20name%20of%20the%20specialized%20template.%0A%09%20*%20%40param%20array%20%20%20%20%20%20%20%24args%20Additional%20arguments%20passed%20to%20the%20template.%0A%09%20*%2F%0A%09do_action(%20%22get_template_part_%7B%24slug%7D%22%2C%20%24slug%2C%20%24name%2C%20%24args%20)%3B%0A%0A%09%24templates%20%3D%20array()%3B%0A%09%24name%20%20%20%20%20%20%3D%20(string)%20%24name%3B%0A%09if%20(%20”%20!%3D%3D%20%24name%20)%20%7B%0A%09%09%24templates%5B%5D%20%3D%20%22%7B%24slug%7D-%7B%24name%7D.php%22%3B%0A%09%7D%0A%0A%09%24templates%5B%5D%20%3D%20%22%7B%24slug%7D.php%22%3B%0A%0A%09%2F**%0A%09%20*%20Fires%20before%20a%20template%20part%20is%20loaded.%0A%09%20*%0A%09%20*%20%40since%205.2.0%0A%09%20*%20%40since%205.5.0%20The%20%60%24args%60%20parameter%20was%20added.%0A%09%20*%0A%09%20*%20%40param%20string%20%20%20%24slug%20%20%20%20%20%20The%20slug%20name%20for%20the%20generic%20template.%0A%09%20*%20%40param%20string%20%20%20%24name%20%20%20%20%20%20The%20name%20of%20the%20specialized%20template.%0A%09%20*%20%40param%20string%5B%5D%20%24templates%20Array%20of%20template%20files%20to%20search%20for%2C%20in%20order.%0A%09%20*%20%40param%20array%20%20%20%20%24args%20%20%20%20%20%20Additional%20arguments%20passed%20to%20the%20template.%0A%09%20*%2F%0A%09do_action(%20’get_template_part’%2C%20%24slug%2C%20%24name%2C%20%24templates%2C%20%24args%20)%3B%0A%0A%09if%20(%20!%20locate_template(%20%24templates%2C%20true%2C%20false%2C%20%24args%20)%20)%20%7B%0A%09%09return%20false%3B%0A%09%7D%0A%7D” highlight=”” provider=”manual”/]

What you will notice is that it comes handy with hooks —meaning that if needed then the developer has just super-powered your code for you already. There is now less lines to type as well, meaning that overall it will save extra time in getting the code out and maintained, meaning that development costs can be simplified.

What can also be done is to put the data within the get_template_part() function providing an opportunity to not have to declare it. You can, however, also declare it as an empty one, so that when you get into the specified template part, the data can then be managed through there. The possibilities have just increased! 

This provides more context into the variable that it is to be passed within the specified template part, which is the perfect location rather than being called outside the function because that might signify that it is meant to be part of the parent template instead (which isn’t always the case).

More exciting WordPress core updates on the way

This is just one of the many changes found within the WordPress 5.5 update. And that this has been dubbed as the best WordPress update ever. There’s definitely more potential for additional updates in the future, and glad that the updates are getting even more exciting as time goes by!

If it’s available on WordPress — use it to your advantage!

...