Integrating Wordpress into Smarty

Published:
May 16, 2007 @ 11:49 pm
Category:
PHP
Author:
Chris Wheeler

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!

9 Responses to “Integrating Wordpress into Smarty”

  1. pranay Says:

    Hello,

    We can integratting WPMU with smarty …I saw example with wordpress but not WPMU. If this is possible can you help on how to.

    Regards,
    Pranay

  2. Toure Says:

    Many thanks…
    This is exactly what I needed.
    But will both be 100% integrated, I guess I will have to figure that out for myself.
    Thank you.

  3. Pete Williams Says:

    Looks good but how do you get all the blog variables, like title etc into you Smarty templates?

  4. Elektronaut Says:

    This is a great tutorial.. Really helped me a lot with the development on my new website.

    However, I have one small thing I cannot seem to get working. Since my Smarty setup uses a map called pages (which holds my php pages) the url re-writing for permalinks won’t work. I’ve tried a couple of things, but couldn’t get it working :(

    Could you please help me out here?

    So instead of www.domain.com/blog.php i have www.domain.com/pages/blog.php

    Thanks!

  5. Niels Says:

    Thanks for sharing! I spend a while trying to enable Worpress integration in a ECommerce System using Smarty, this post helped a lot :)

  6. PHK Corporation Says:

    A Smarty for Wordpress plugin is NOW available for download at http://wordpress.org/extend/plugins/smarty-for-wordpress/ this is NOT a hack but a modification of the latest smarty version to work under wordpress.

  7. Wordpress integrieren - Seite 2 - JTL-Software Supportforum f Says:

    […] eine Anleitung, wie man eine Wordpress Installation in bereits vorhandene Smarty Inhalte einf

  8. eleven Says:

    Great starting point, but I think that in the sake of example, you should have made this based on a RAW installation of wordpress, as in “wp-content” / “wp-include” directory structure.

    I’ve been working on this all morning, and i’ve found a simplier solution:

    1- Add this to your smarty PHP calling the blog:

    define(’WP_USE_THEMES’, false); // NOTE the ‘FALSE’ here!!!!
    require(’../wordpress/wp-blog-header.php’);

    2- Add this to your TPL

    {include_php file=’wp-content/themes/classic/index.php’}
    // Change to the path of your structure

    3- change the BLOG URL in the WP Admin area > General Options
    You need to make this URL relative to your page content, so you won’t have bad links!

    Hope it helps!

  9. Chris Wheeler Says:

    Hi Evelen,

    Thanks for the comment - this article was written back in 2007 so I think the WordPress code may have changed a bit - hopefully your comments will be of use to others wanting to do this - I’ll update the post to direct people to the comments as there has been some other good points raised.

    - Chris

Your Details

(Will not be published)



Comment