Home  »  ArticlesGuidesHow ToProgrammingTechnology   »   How to Pass Variables via get_template_part in WordPress

How to Pass Variables via get_template_part in WordPress

As a WordPress theme developer, sooner or later, you will encounter a situation where you need to declare a variable in one file and use it in another included one and find that you cannot directly do it. The good news is that you can pass variables via get_template_part in WordPress using two methods that we will be highlighting here.

Option 1: Passing Variables via get_template_part Using WordPress 5.5+

As of WordPress 5.5, passing variables via get_template_part is now part of the core. Previously you could only pass two parameters, slug, and name. Starting in WordPress 5.5, the template loading functions allow additional arguments to be passed through to the matched template file using a new $args parameter as the third parameter.

This is the syntax:

get_template_part( string $slug, string $name = null, array $args = null );

Here is an example of how to use the WordPress get_template_part function:

get_template_part( 'template-parts/geometrical-entities', null, array( 
  'class' => 'shape-box',
  'data'  => array(
    'component_type' => 'shape',
    'component_name' => 'rectangle',
  )) 
);

In the included template file (i.e. template-parts/geometrical-entities), you can gain access to the variables as show below:

<div class="widget <?php if ( $args['class'] ) { echo esc_html( $args['class'] ); } ?>">
    <?php if ( $args['data'] ) { echo esc_html( $args['data']['component_name'] ); } ?>
</div>

Alternately, for cleaner code, you can set up defaults first, using wp_parse_args:

  // Setup defaults
  $array_defaults = array( 
    'class' => 'featured',
    'data' => array(
      'component_type' => 'shape',
      'component_name' => 'circle',
     )
  ); 

  $args = wp_parse_args( $args, $array_defaults );

Option 2: Using set_query_vars

This option is good if you have older versions of WordPress. It is used to set the value of a query variable in the WP_Query class with the following syntax.

set_query_var( string $key, mixed $value );

With this you set the variable in your theme file as follows:

set_query_var( 'class_name', 'team-profile' );
get_template_part( 'template-parts/team' );

In your template-part/team file you can retrieve the variable like so:

<?php
  $myVariable = get_query_var( 'class_name' );

  if ( $myVariable ) {
?>

<div class="widget <?php echo esc_html( $myVariable ); ?>">
    ... some html here
</div>

<?php
  }
?>

Note: The variable set by set_query_var stays in the globals. You will need to:

set_query_var('class_name', false); 

After the get_template_part call otherwise it will be available in the next call of the template.

In Closing

These two methods are the proper ways to variables via get_template_part in WordPress. The choice of which one to use is a matter of preference as well as the oldest version of WordPress that you intend to support as a plugin or theme developer.

Ref: [ 1 ],[ 2 ]

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.