How to Make WordPress-generated Pages Outside the WordPress Install Directory
I wanted to use my blog entries, created in WordPress to generate content for pages on my site that were not in my blog. This seemed simple enough, and it is, but without some guidance this can be a really confusing situation. While WordPress supports a pseudo-templating system that can be used to easily customize the look and feel of a WordPress blog, getting that blog-portion of the site to play nice with the other parts of a site (or the converse, as the case may be) is often not as easy as it seems.
I believe this is one of the reasons why WordPress’s popularity has not yet reached its enormous potential.
But without further ado, here’s what I did to get WordPress to generate pages in a directory completely outside WordPress’s own installation directory.
The Solution
The solution involves replacing all occurences of WordPress’s erroneous absolute link-creation output and replace it with output appropriate to the /newsection structure. This was done with PHP’s output buffering. Then, to make it work with the permalink structure, I made some edits to the WordPress-generated RewriteRules. In the hopes that this will be of use to someone, here’s what I did:
-
In the top of your new section’s template (/newsection/index.php), following the line with the
require()statement, put a new line that reads:ob_start();
This will start the output buffer.
-
At the very bottom of your template, after the
</html>add the following lines:<?php $page_content = ob_get_contents(); $replace_this = 'href="http://mysite.com/blog'; $with_this = 'href="http://mysite.com/newsection'; $modified_page_content = str_replace($replace_this, $with_this, $page_content); ob_end_clean(); echo $modified_page_content; ?>
These do the replacements you need. Obviously, change the
mysite.comto whatever your site domain is, change the/blogto whatever path your WordPress install is at, and change the/newsectionto the path of the new section you’re creating.With these lines wrapped around your template, you can use all of WordPress’s original template tags like
the_permalink()andwp_list_cats()just as you would if you were using them in the original section.Note to regex wizards: this is obviously a very crude string replacement, and I don’t know how well it will work in situations not like my own. I’m not very good with regular expressions, however, so if you can think of one that would work better with
preg_replace()please let me know. -
If you’re using WordPress’s virtual site structure (pretty-permalinks): Copy the rules WordPress wrote for you at Options->Permalinks (or from your existing
.htaccessfile) and paste them into a new text document. This will become your new section’s.htaccessfile.-
In the line that begins with
RewriteBasereplace the old base with the new base path to your section. If the line readRewriteBase /blog/then you would replace/blog/with/newsection/. -
Finally, for the second, third, and fourth lines that begins with
RewriteRulereplace the old actual path with the actual path to your new section. So, continuing the example above, if you have a line that reads like this,RewriteRule ^category/?(.*) /blog/index.php?category_name=$1 [QSA]
replace it with this:
RewriteRule ^category/?(.*) /newsection/index.php?category_name=$1 [QSA]
-
Save this file as a new
.htaccessfile and upload it to your server so that it resides athttp://mysite.com/newsection/.htaccess
-
No matter what section/directory of your site you’re putting WordPress templates in, you can use this method to point links to the current section, and thus load WordPress content in the templates within that section/directory. One important caveat to this method is that every time a template using this method is accessed, your server works a little over-time to do the str_replace() on your content. This won’t be noticeable on small or short pages, but could really become an issue on oft-visited, long, long pages. As always, use at your own risk.
Improvements/warnings/suggestions/hate mail are welcome.
External Resources
- The original support forum thread.
- The Wiki HowTo node I wrote.
- WordPress Customization Notes.