Last Updated: February 25, 2016
·
7.182K
· pimschaaf

Passing variables to get_template_part in WordPress

Use the WordPress locate_template() function within PHP’s include(), instead of get_template_part().

<?php
$var = 'Hello World!';
include(locate_template('file/path/filename.php'));
?>

In file/path/filename.php:

<?php
echo $var;
?>

returns:

Hello World!

Credits: Keith Devon

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

WordPress sites collect scripts from themes, plugins, and tag managers. A compromised plugin update can add a skimmer to every page without a change in your own templates.

Security insights from Cside