Categories: General

Genetics and Prejudice

clean-coding-best-practicesclean-coding-best-practices

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.

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:

  1. Get a list of all your links

  2. Pull your tag cloud from your posts

  3. 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
daniel@danielmiessler.com
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
]

Gerald Businge

Leave a Comment
Share
Published by
Gerald Businge

Recent Posts

Mary ICT teacher Ordinary secondary

Meet Mary — Your Smart AI ICT Teacher for O-Level Secondary Education in Uganda! Welcome…

2 weeks ago

Form 3 and Form 4 Islamic Religious Education notes

Journey Through Faith: An Introduction to Senior Three Islamic Religious EducationWelcome, students, to an enriching…

2 weeks ago

Isha Karungi

Meet Isha Karungi — Your Smart AI Islamic Studies Teacher for O-Level! Welcome to the…

2 weeks ago

Dulce de Leche Granizado Ice Cream

Having studied Spanish for over 6 years, I knew what dulce de leche meant. Sweet.…

2 months ago

Kiwi Lime Sorbet

I found kiwis on sale. Five for $1! In the middle of winter. In January.…

2 months ago

Ghirardelli Chocolate Market Street San Francisco

An experience. That’s what Ghirardelli is to me. For many years, San Francisco was a…

2 months ago