I'm creating a custom plugin which needs to display HTML with dynamically updated values.

To illustrate, here's what I want to achieve:

<h1><?php print $title; ?></h1>
<h3><?php print $subtitle; ?></h3>
<div class="description"><?php print $content; ?></div>

How can I define a template, pass some variables and get output in return from my custom plugin? I do not want to embed everything as a variable name as it's messy; going back between PHP and HTML in a middle of a function doesn't seem any cleaner either.

What is WP way of doing this?



The various styles of mixing and matching HTML and PHP has always been a bear. Circumstances sometimes dictate the method you use, but many developers prefer one style or another and WordPress tends to use the style in your example.

Another option, which is a bit cleaner in my opinion is to make use of the heredoc syntax in PHP which allows for simple variable substitution. http://php.net/manual/en/language.types.string.php

$Output = <<< EOF
<div class="description">$content</div>

return $Output;

You can't do away with the PHP variables entirely, but you can make it easier to write, read, and maintain.

  • Thank you for your response. I also have seen some plugins simply including the so-called view file which just uses variables defined earlier in the function. That one seems the cleanest of all, even though still feels hackish. Is that recommended to be used? – Aram Boyajyan Nov 6 '12 at 20:09
  • I'm not aware of WordPress actually stating a preference, but as I said the whole WordPress community of developers tends to use the inline style that you have as an example. Just keep in mind that the heredoc style does have it's limits and won't work all of the time. – Steve Nov 6 '12 at 20:13

I'm in agreement with the OP that Wordpress plugin authors have traditionally mixed business logic and view markup to a great degree, and I think continue to do so despite strides made with PHP frameworks. I'm relatively new to WP development, but as a programmer, I thought a simple fix for this would be in order. Thus, I hope it is acceptable to mention here that I've been working on TemplateSystem, a lightweight templating system that works well with WP plugins.

As Murphy's Law would have it, I released this code and then found that there are several libraries that will do WP plugin templating - my advice is to try several and see which you get on with. Mine is very lightweight, and some people will either use it or reject it on that basis!

Here's an example of an entry point to a plugin I am working on at the moment, that uses this templating system:

# plugin/main.php
$root = dirname(__FILE__);

require_once $root . '/vendor/TemplateSystem/ControllerBase.php';
require_once $root . '/controllers/VersionedCommentsController.php';

$controller = new VersionedCommentsController( $root );

Here's some controller code:

# plugin/controllers/VersionedCommentsController.php

// Import this class as a unique name
use TemplateSystem\Change2\ControllerBase as VersionedCommentsControllerBase;

class VersionedCommentsController extends VersionedCommentsControllerBase
     * Main controller entry point (initialisation here)
    public function execute()

        add_action('add_meta_boxes_comment', array($this, 'editCommentDialogue'));

     * This is a WP action handler
    public function editCommentDialogue(stdClass $comment)
            // Snipped detail, the important thing here is the rendering

                    array('commentVersions' => array_reverse($commentVersions), )

    // Lots of other methods here, usually protected

A snippet of the template layer (shortened for brevity):

<!-- plugin/templates/version-history.php -->
            <div class="comment-items">
                <span class="comment-item">
                    Name: <?php echo $commentVersion['comment_author'] ?>
                <span class="comment-item">
                        echo $commentVersion['comment_author_email'] ?
                        $commentVersion['comment_author_email'] :
                <span class="comment-item">
                        echo $commentVersion['comment_author_url'] ?
                        $commentVersion['comment_author_url'] :

There's partials and component support too; see the README if you want to know more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.