While searching for a way to integrate WordPress into an existing web site, all the tutorials I came across were aimed at users who wanted to make WordPress look like their existing site. There were also some tutorials on how to take parts of the WordPress engine and add them to existing PHP pages, however; I couldn’t find anything on integrating WordPress with Smarty, so I thought I’d have a go - and it turned out a lot easier than I thought.
Step 1: Install WordPress
Download WordPress and install to a directory on your web site - this will be the final url which visitors will see. I chose /blog/
Step 2: Create custom PHP includes
Now you need to create two PHP files, which provide ‘The Loop’ (the code which provides the content for all blog pages) and the sidebar links. Credit for this goes to the author of this article.
The two files I created were:
/blog/custom/the-loop.inc.php
View the-loop.inc.php
This is basically just /blog/wp-content/themes/classic/index.php with get_header() and get_footer() removed.
/blog/custom/sidebar.inc.php
View sidebar.inc.php
This is basically just /blog/wp-content/themes/classic/sidebar.php
You could most probably just include the sidebar.php file from the classic theme, but I wanted to remove a few sections
Step 3: Include the WordPress header
The WordPress header file needs to be included on all pages which have blog content on them, I simply placed it into my ‘header.php’ file - as this is included on all pages
require_once './blog/wp-blog-header.php';
Step 4: Create the Smarty PHP files
To show the main WordPress content inside of your sites design, you need to create a PHP file and template. My file looks something like the following:
blog.php
View blog.php
The $page variable is simply an instance of the Smarty class, which is instantiated in the header.php file, the page title is set and then the blog.tpl file is parsed.
The ‘if’ statement checks to make sure the page isn’t a Rss, Trackback or Robot - as in these cases we do not want to send any html output, as output has already been created by the WordPress header.
Step 5: Create the Smarty TPL templates
To pull in the main WordPress content area into your smarty templates, use the following:
blog.tpl
{include file="html_header.tpl"}
{include file="layout_header.tpl"}
<h1>Blog</h1>
{include_php file='blog/custom/the-loop.inc.php'}
{include file="layout_footer.tpl"}
{include file="html_footer.tpl"}
The html_header, layout_header, layout_footer and html_footer template files contain the XHTML which provides the layout for my site.
You can include the sidebar content anywhere on your side, but keep in mind that it can become quite long, and have multiple nested lists depending on how you set up your categories.
sidebar_blog.tpl
{include_php file='blog/custom/sidebar.inc.php'}
Step 6: url re-writing with .htaccess
To make sure your blog.php file is used instead of the default /blog/index.php, add the following .htaccess rules.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)blog$ $1blog/ [R=301,L]
RewriteRule ^(.*)blog/$ $1blog.php [L]
If you want to use URL re-writing for permalinks within WordPress, add the following lines to your .htaccess after the above lines
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)blog/(.*)$ $1blog.php [L]
You should with any luck now have WordPress integrated into your Smarty powered site!