Integrating WordPress into dynamic templates

I installed a WordPress blog on my development server the other day and began playing with it. The first real challenge I faced was how to pull my WordPress installation into my Web site’s template.

My issue is, I’m using a content management system (CMS) to manage the bulk of my Web site’s content. However, I wanted to use WordPress to manage my various blogs. I obviously wanted my blogs to look like the rest of my Web site, so I needed to come up with a plan to integrate my WordPress installation into my CMS, somehow.

Basically, what it came down to was that I needed to find a way to store all of my WordPress output into PHP variables. Once I had done that, I could plug those variables into my template. The main problem I ran across, however, was the fact that 99% of the functions WordPress uses to build its output utilize echo commands rather than simply returning the output.

That was no good for me, obviously, as it started printing content onto my page before the template had been processed.

PHP came to my rescue, and with very little headache. PHP’s output buffer was the simple answer to my problem.

Within PHP, you can create an output buffer that captures everything that would normally be printed onto your screen. Then, you can choose to either print that information or store it into a variable. In fact, if you want to get really fancy, you can even store it into multiple variables along the way (but I didn’t really need to do that).

So, I set to work using the output buffer. I had to hack my way through the WordPress template in order to get it working properly.

With my particular Web site template, I can set a series of variables, including:

  • Page title
  • Section name (for use as the header of my section navigation menu)
  • Section menu (basically the sidebar of my template)
  • Page content
  • Page head (style definitions, javascript declarations, etc.)
  • stylesheet (links to my various stylesheets)
  • Main template file (the bulk of the template information that should be displayed on every page – similar to headers and footers that people commonly use)
  • Inner template (the template file to use inside of the “content” area)
  • … and quite a few more

I set to work building a WordPress template to get all of those variables from WordPress and send them to my template.

First, I had to include the file that builds my main Web site template (includes a bunch of classes and functions, etc. that pull various information from different places and puts it all into the appropriate places). That was easily achieved with a simple require_once statement.

Next, I had to capture the title of the WordPress page. I used the following syntax for that:

get_bloginfo('','display').wp_title(' - ',false)

Notice the “false” as the second parameter of the wp_title function call. The wp_title function actually gives you the option whether to echo the results or simply return them. If you set the second parameter to “false”, it will return the output rather than echoing them. By default, that parameter is set to “true”, so it usually just echoes the output.

The “hard part” came next. I had to start an output buffer to catch all of the output from WordPress. I did so by using the ob_start() function that’s built into PHP.

Once I started the output buffer, I used the standard WordPress output functions (found inside any “index.php” WordPress template file) to build the content of my page.

Once the output finished, I stored the output in my “pagecontent” variable by using the ob_get_contents function. I then cleaned out the output buffer and closed it.

Next, I opened a new output buffer and invoked the “get_sidebar” function. I stored that output to another variable (my “secmenu” variable) to be used as my section menu (or sidebar) in my Web site template.

I then cleaned out and closed the buffer again.

Finally, I declared my template file and my inner template and called my buildtemplate function (the function within my CMS that builds the template, putting all of the content where it belongs). That was all there was to it.

My WordPress template ended up looking something like:

<?php
require_once('/home/youruser/public_html/cmsdirectory/config.php');
$blogpage = new template;
$blogpage->pagetitle = get_bloginfo('','display').wp_title(' - ',false);
ob_start();
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php the_date('','<h2>','</h2>'); ?>

<div class="post" id="post-<?php the_ID(); ?>">
<h3 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<div class="meta"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — <?php the_tags(__('Tags: '), ', ', ' — '); ?> <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></div>

<div class="storycontent">
<?php the_content(__('(more...)')); ?>
</div>

<div class="feedback">
<?php wp_link_pages(); ?>
<?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
</div>

</div>

<?php comments_template(); // Get wp-comments.php template ?>

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

<?php posts_nav_link(' — ', __('« Older Posts'), __('Newer Posts »')); ?>
<?php
$blogpage->pagecontent = ob_get_contents();
ob_end_clean();
ob_start();

get_sidebar();
$blogpage->secmenu = ob_get_contents();
ob_end_clean();

$blogpage->templatefile = 'template.html';
$blogpage->innertemplate = 'maininner.template';
echo $blogpage->buildtemplate();
?>

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

One Response

  • gaetano

    I am interested in doing something like this. Do you have the code for the all this work?
    The main template file and the inner template and the buildtemplate function ?

    Sounds like you adjusted the function.php with a routine and called the main template to be the master as the inner does all the work?

    I need to do something like this where I have one template and one page in work press. And all the NEW php pages are created and sent to this main template. Since the landing page is the main page I wanted to call the sub-page as dynamic.
    Example:

    Main-page/Sub-page1 – as PHP and sql return output
    Based on links or new output that is different it would return or send to another page as
    Main-page/Sub-page2 – as PHP and sql return output for new information requested?

    Can this be done without creating a new page in word press for all my SQL reports in (TABLE HTML Format). I estimate that I have over 15 pages.

    Let me know if this is achievable

Post Your Comment

Your email address will not be published.