More about WordPress integration with custom sites

Yesterday I did a lot of work to make WordPress and my custom site word seamlessly one with the other. Today I’m trying to raise that level of integration to an higher level. I had 2 problems with a common solution:

  1. Am I the admin? I won’t be creating a backoffice anytime soon, but I would still like to have some admin-only features, why not use WordPress login system?
  2. At my home page I’ll probabily want to show my latest blog posts, how to bridge the 2 sites without having to extend my WordPress templates?

It turns out that the internals of WordPress are a little bit easier than I expected, it took only a few minutes to realize this and have both problems solved.

To start with, I use the excelent Google Analyticator plugin to automatically include my Google Analytics code at each page’s footer, it’s in fact so good that it even doesn’t include the Analytics code if I am logged in. That was also my objective to the rest of my site, remove the Analytics code if I’m logged in with WordPress. So, at the end I just included this in my pages template:

    if(!current_user_can(’level_8?)) { ?>
        <!-- your Google Analytics code goes here -->
<?     } ?>

Since I was using a Dreamweaver template to manage both my WordPress and custom pages I also added a conditional template variable, I called it “disableAnalytics” and defaulted it to false, then I just overrided it on all the theme’s file to true because in those pages Google Analyticator will handle it (the benefit of still using the plugin is that if Google changes the script’s code, updating the plugin is way easier).

The special thing about that code is the include I made, the wp-load.php script will load and setup several variables for a WordPress environment, still it is lighter and faster than using a fully managed page. Calling current_user_can(something) is the real work horse here, it simply checks if a user has permissions for something like writing posts or, in this case, if a user can do operations of at least level 8. WordPress manages user’s permissions based on levels, the owner usually is the “admin” and should have level 10, but since you can add more users and different groups of users you can setup different levels to help plugins manage the level of trust in each user. Anyway, since one could have logged in users that aren’t actually admins I decided that leaving it to a level 8 leaves room for some extra admin slots.

But what if you need more advanced features from WordPress? A widget perhaps? To use a plugin (I could “reuse” Analyticator for example)? Well, in my case I wanted to include a small list of blog posts, but no matter what you require, what you really need is to call:


It does load wp-load.php by itself but since I’m using require_once on both snippets it won’t make any kind of trouble. Besides wp-load.php this last script also does several initializations and theme loading things. That means that to the best of my knowledge and testing, you should be able to start using any function from the WordPress internals. So, I created a new category to my posts called “featured” and used this code to get a list of featured items on my home page:

    if(!current_user_can('level_8')) { ?>
        <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
        <script type="text/javascript">
        try {
        var pageTracker = _gat._getTracker("UA-253850-1");
        } catch(err) {}</script>
<?     } ?>

That was way easier than expected.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , ,

Leave a Reply

For spam filtering purposes, please copy the number 6414 to the field below: