- Unsupervised Learning
- Posts
- How to Include WordPress Components in Your Custom Web Pages
How to Include WordPress Components in Your Custom Web Pages
Ever wanted to take a piece of WordPress functionality and put it in a non-WordPress portion of your website? Like, for example, pulling your WordPress Sidebar and dropping it into a custom page of yours?
Well, here’s how to make it work.
The Wrong Way
Many have tried doing this simple include below and have met with failure. What they did was add the include by itself, like so:
wp_get_archives('type=monthly');
This ends in a fail whale because PHP doesn’t know what to do with that request. Within a WordPress page it’s fine because you already have the required headers loaded, but in your own custom web site they aren’t there.
So we’ll add them.
The Right Way
// turn off WordPress themes and include the WordPress core: define('WP_USE_THEMES', false); require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php'); wp_get_archives('type=monthly'); ?>
If you include the WordPress blog header first (wp-blog-header.php) and then invoke the piece of code you want, you’ll be able to add WordPress functionality anywhere you want on your custom pages!
The code above (get_archives) allows me to display my entire blog archive on any page of mine I want — even outside of a WordPress-generated page.
Unsupervised Learning — Security, Tech, and AI in 10 minutes…
Get a weekly breakdown of what's happening in security and tech—and why it matters.
This lets you get tons of functionality from WordPress, including:
Get a list of all your links
Pull your tag cloud from your posts
Pull your WordPress sidebar into a page
…and there are dozens more here.
This is how I have created the page you’re reading this on. The entire page is custom code; I only have a couple of pieces of WordPress embedded within it — the loop, and my archives toward the bottom on the right. This gives me the ability to seamlessly blend my WordPress content with my custom content at danielmiessler.com/study and danielmiessler.com/writing.
It’s the perfect combination — custom control + WordPress functionality.
Ping me at [email protected] with any questions…
Links
[ WordPress’s get_archives | wordpress.org ][ WordPress Template Tags | wordpress.org ][ The WordPress Loop | wordpress.org ][ The WordPress Codex | wordpress.org ]