Skip to main content
Dublin Library

The Publishing Project

Passing data to WordPress template files

 

The template loading functions (get\_header(), get\_template\_part(), etc.) have a new $args argument. So now you can pass an array’s of data to the templates. The idea is that we can now pass an object with additional, arbitrary, information for the template to use. In this example, we load a hypothetical `foo` template part and pass an array of items as the third parameter on the function call. ```php 'user', 'arbitrary_data' => array( 'foo' => 'baz', 'bar' => true, ), ) ); ``` We can then use this arbitrary data on the template. The example uses the `class` attribute from the `$args` array and from `arbitrary_data` we take the `foo` child element using a nested array syntax. ```html
``` We can further enhance the customization using conditional tags so that only some templates use the custom variables we loaded the template with. The functions that take the third argument are: The affected functions are: 1. [get\_header()](https://developer.wordpress.org/reference/functions/get_header/) 2. [get\_footer()](https://developer.wordpress.org/reference/functions/get_footer/) 3. [get\_sidebar()](https://developer.wordpress.org/reference/functions/get_sidebar/) 4. [get\_template\_part()](https://developer.wordpress.org/reference/functions/get_template_part/) 5. [locate\_template()](https://developer.wordpress.org/reference/functions/locate_template/) 6. [load\_template()](https://developer.wordpress.org/reference/functions/load_template/) For more information see [Passing arguments to template files in WordPress 5.5](https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/) in the Make WordPress Core blog.

Edit on Github