Creating Custom Templates and Passing Variables

In this document we’ll look at creating a custom Text Blocks template in your theme so you can do whatever you want with your reusable text block. This currently only works when using the shortcode.

Let’s say you want to create a shortcode like this:

[text-blocks id="198" name="Ronaldo" template="hal"]

You want to pass in the variable Ronaldo into your template and it would change out the place holder in your text block with that text.

The first thing you need to do is add the template attribute. In this case I used ‘hal’ as my template name. You will need to add a file to your theme folder called text-blocks-hal.php where -hal is the variable you used in the template attribute.

Screen Shot 2015-04-07 at 1.08.30 AM


This template is going to be passed a few variables that allow you to do what you need:


This variable contains the content of the Text Block you edited in the admin under ‘Text Blocks’

Screen Shot 2015-04-07 at 1.23.58 AM

Our example has a simple sentence with a little curly bracket variable we made. In your template you would need to run: <?php echo $content; ?> just to spit that out as plain text.

Screen Shot 2015-04-07 at 1.18.09 AM

If you need the shortcode to activiate just wrap $content with do_shortcode: <?php echo do_shortcode($content); ?>

This variable is an array of all the attributes that are passed to the template. You can use this however you need.

Screen Shot 2015-04-07 at 1.13.37 AM

To replace our {player} variable we can use a simple str_replace: <?php echo str_replace("{player}", $atts['name'], $content); ?>

Screen Shot 2015-04-07 at 1.19.48 AM$id
The id of the text block is passed in with this variable.