logomichael sumner
Contact

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()

function get_template_part( $slug, $name = null, $args = array() ) {
	/**
	 * Fires before the specified template part file is loaded.
	 *
	 * The dynamic portion of the hook name, `$slug`, refers to the slug name
	 * for the generic template part.
	 *
	 * @since 3.0.0
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string      $slug The slug name for the generic template.
	 * @param string|null $name The name of the specialized template.
	 * @param array       $args Additional arguments passed to the template.
	 */
	do_action( "get_template_part_{$slug}", $slug, $name, $args );

	$templates = array();
	$name      = (string) $name;
	if ( '' !== $name ) {
		$templates[] = "{$slug}-{$name}.php";
	}

	$templates[] = "{$slug}.php";

	/**
	 * Fires before a template part is loaded.
	 *
	 * @since 5.2.0
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string   $slug      The slug name for the generic template.
	 * @param string   $name      The name of the specialized template.
	 * @param string[] $templates Array of template files to search for, in order.
	 * @param array    $args      Additional arguments passed to the template.
	 */
	do_action( 'get_template_part', $slug, $name, $templates, $args );

	if ( ! locate_template( $templates, true, false, $args ) ) {
		return false;
	}
}

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!