Accessing WordPress functions from external PHP pages

by Ed Gately on December 9, 2009 · 35 comments

in Wordpress Advanced,Wordpress General Tips,Wordpress Software

So a friend of mine shoots me an email today.  She had a front end website, just set of basic pages built with PHP and one of the links on the navigation was a WordPress blog installed on the same hosting.  She wanted to display an RSS feed in the sidebar of the front end website by using a nifty little script called Simple Pie.

Looking over the script, I found it to be a handy little script.  It pulls an RSS feed and parses it easily for you and then you can output the parts you want with links to the full article.  The only problem I found with what she was trying to do, she just wanted to parse the RSS feed of the WordPress blog on the same site and display the last 5 articles on the home page of the primary site.

This is easily done with about 8 lines of code.  You can get full access to everything WordPress has to offer from external PHP pages on the same site.

The first thing you need to do is include a WordPress file in order to load the functions you’ll need access too:


The above code is assuming that your installation is in the “blog” directory, naturally change it to reflect your installation.  Once this is included in your script you can access all the WordPress functions.  Now if we expand on that code as follows:

query_posts('showposts=5'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

Now if you plug through the code you’ll see that we are pulling the last 5 posts (showposts=5) and then we are looping through that information and writing it in HTML.  Surround each title with an H2, or your own choice, and create a link with the appropriate WordPress permalink.

So instead of messing with an entire separate class or script we’ve included the WordPress functions, then just used the functions to display the information we already wanted.  Keep it simple.  Use the tools you already have available to you.

The main lesson I find with this example is that there is always 101 ways to skin a cat.  The idea is to make sure you are using the tools already available to you, and implementing the solution that keeps it simple.  Don’t get focused in on a solution and waste a lot of time, and never hesitate to ask for help.

Related posts:

  1. Want to add a Widget to your WordPress Blog?
  2. Redirecting Feed to Feedburner with htaccess
  3. Are you suffering from WordPress Plugin overload?
  4. htaccess Rules for WordPress Permalinks
About The Author: Hi, I'm Ed Gately and I write Wordpress Master to help bloggers get started in building a successful online blog using Wordpress. Prior to writing on this blog I spent over 16 years working in Corporate IT and have been spreading my wings on the web for 3 years. To learn more about this website and me visit About Wordpress Master.

{ 34 comments… read them below or add one }

Alan Sheu July 12, 2010 at 2:29 pm

Hi Ed,

Thank you very much for providing the article. I know this post is rather old, but I hope you might be able to help me out with implementing the script you’ve outlined. I’m not having any success with the require_once() method, and I am not sure what the problem is, since it seems so straightforward.

At the moment, I’m using JQuery to make an Ajax call to “custom_functions.php”. I don’t want to add the new function definitions directly to the theme’s original “functions.php” because I need to pass specific information asynchronously and have the server return new DOM elements based on the information given it.

The error I see is:
“Fatal error: Call to undefined function query_posts() in /..filepath../custom_functions.php on line 23″

I know that the line “require_once(‘./filepath…/blog/wp-blog-header.php’);” is being executed correctly, since there is no fatal error caused by the require_once() method (I kept getting those until I correctly inputted the link). At least I THINK it’s safe to assume that the wp-blog-header.php is being imported into the new document.

Do you have any ideas on how I can fix this? I am at a complete loss. Any help would be greatly appreciated!


Ed Gately July 14, 2010 at 12:04 pm

Could you tell me a little more about the JQuery/AJAX call you are making to the Custom_functions.php file?

There are several references on the web related to the require_once() implementation having some issues. You will notice the solution in my post is all self contained. You may have an issue with Wordpress Globals not being passed outside the active function.

Here is one such reference: Calling wp-blog-header.php from inside a PHP function


Jason September 4, 2010 at 1:06 pm

just thought I’d add my 2 cents!

On the site I am currently working on, I am using WordPress as the CMS….

Backend (wordpress):
-login as admin
-add categories for your page, and page sections
(i.e. home, home-top,home-top-left,etc…wherever you want controllable content)
-add content(posts) to your categories
–posts->add new
—title->something distinctive(Home: top content)
—category(select the category you want the article to belong to (home-top))
—write article

Frontend (the website):
- at the top of pages that use wp blog entries
( )
-here is the code you place in whatever section
( div ‘home-top’ being the container id same as the post category for readability…can be any id, or container for that matter)

Basically I am able to control every section of my site through word press (content wise) and by using the user permissions module I am also able to use the blog capabilities as well by hiding all the section content from all but admin…anyway I thought it was cool, peace out.


Web Hosting October 5, 2011 at 8:21 am

When I follow your 8 lines of code and use them strangely my browser get redirected to the wp-admin/install.php page…


Adam Luz July 24, 2012 at 11:22 am

When using the code above you should add header(“HTTP/1.0 200 OK”); after your require otherwise it’s sending a 404 (although the content is still loading) which Google doesn’t like. Also if you integrate the header in something like JSON, a 404 header prevents it from rendering properly.


heel lifts insoles October 8, 2012 at 6:29 am

I wear a necklace, cause I wanna know when
I’m upside down.


veer November 9, 2012 at 12:10 am

Here’s the latest on the conference’s top players who are projected to go from playing on Saturdays to starring on Sundays.

1. LB Jarvis Jones, 6-3, 241, Georgia

Why he has the top spot: Jones’ battle with an ankle injury hasn’t changed his status as the top overall draft prospect in the SEC. He missed his second full game due to the

injury in the Bulldogs’ 29-24 win over Kentucky on Saturday. Still, Jones remains a versatile defender who can rush the pass or drop into coverage. He’ll need a strong second

half of the season to maintain his lofty draft status.

Season stats (five games): 36 tackles, 5.5 sacks, 9.5 TFLs, 3 forced fumbles, 15 QB hurries, INT.

Player ranking: Jones remains fifth overall, and he is the second-highest outside linebacker in’s latest player rankings.

Up next: The Bulldogs’ next game is against Florida. Jones returned to practice on Monday and is expected to play. Georgia will need him against a Florida team that averages 213

yards per game on the ground.

2. QB Tyler Wilson, 6-3, 220, Arkansas

Why he has the second spot: After a slow start for Wilson and the Razorbacks, the senior QB has turned it around in the last few weeks before having a bye last weekend. He’ll

have a chance to solidify his status as a first-round pick with games against Mississippi State, LSU and South Carolina still on the schedule.

Season stats (six games): 124 of 205 (60.5 percent) for 1,957 yards, 14 TDs, 5 INTs.

Player ranking: Wilson is ranked as the No. 12 overall prospect and is the third-highest ranked QB in’s latest player rankings.

Up next: The Razorbacks play Ole Miss this weekend. The Rebels are ranked No. 65 nationally in scoring defense, so Wilson should have a chance to put some good numbers up.


best value starter kit March 5, 2013 at 1:02 am

Ahaa, its good discussion on the topic of this article here at this webpage,
I have read all that, so now me also commenting here.


Astrid May 20, 2013 at 6:11 pm

Greetings! I’ve been reading your weblog for a while now and finally got the bravery to go ahead and give you a shout out from New Caney Texas! Just wanted to tell you keep up the good job!

Reply June 1, 2013 at 9:11 am

Wonderful items from you, man. I have consider your stuff previous
to and you’re simply too excellent. I actually like what you have obtained here, really like what you’re saying and the
way in which wherein you assert it. You’re making it enjoyable and you still care for to keep it wise. I can not wait to read much more from you. That is actually a great website.


pricing soy candles June 26, 2013 at 4:58 am

In fact, despite what a lot of people think, there isn’t such thing as being a “pure” soy pillar candle. You could be tempted to buy cheap scented soy candles because of these price, however you’ll only
be wasting your money and you also won’t truly enjoy everything you’ve bought.
The standard wire-core wick can provide with soy candles.


Aluminieva Dograma June 27, 2013 at 2:22 pm

The other day, while I was at work, my sister stole my apple ipad and tested
to see if it can survive a 30 foot drop, just so she
can be a youtube sensation. My apple ipad is now destroyed
and she has 83 views. I know this is totally off topic but I had
to share it with someone!


islamic cloths July 2, 2013 at 9:01 pm

Hello to all, because I am actually eager of reading this weblog’s post to be updated on a regular basis. It carries good stuff.


cash surveys July 24, 2013 at 6:06 pm

Thanks for your marvelous posting! I quite enjoyed reading it, you might be a great author.

I will ensure that I bookmark your blog and will
often come back at some point. I want to encourage continue your great writing, have
a nice day!


watch dogs Ps3 August 7, 2013 at 11:33 pm

I’m not that much of a internet reader to
be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your site to come back later. Cheers


Joe Fylan September 3, 2013 at 8:08 pm

Wow, wonderful blog layout! How long have you been blogging
for? you make blogging look easy. The overall look of your web site is excellent, as well
as the content!


Carson September 7, 2013 at 10:19 pm

Professional locksmiths can be found round the clock for your locksmith
needs. In a hectic parking spot, the smart key might help to recognize the location with the car developing a distinct sound with
flashing of lights by the click. With all the important roles played with
a locksmith, it’s high time for it to regard them as key players inside the community.


Trace Mobil October 1, 2013 at 9:16 am

You’ve made some decent points there. I looked on the internet to learn more
about the issue and found most people will go along with your views on this website.


Teodoro July 7, 2014 at 6:40 pm

I just like the valuable information you supply on your articles.
I’ll bookmark your weblog and test again here regularly.
I’m rather certain I will be informed many new stuff right right here!
Good luck for the next!


Soft Maple Level Quarter Turn September 13, 2014 at 9:45 am

A bathroom that looks good and functions well is a fantastic place to be
in. Expect to spend at the least $10,000 when you choose brand new low-end cabinetry, counters, appliances, flooring,
paint, and a few minimal structural alterations. Other technological advancements include the development of sensors
that allow buildings to know the strength of tremors and respond appropriately, including the activation of
tremor or movement damper systems that act like shock
absorbers, letting buildings withstand stronger tremors without collapsing.


film o gratuit September 23, 2014 at 5:08 am

Je termineraі ɗe regarder tout cela plus tard


Alexis Silver September 24, 2014 at 12:25 pm

Très bbon poste, j’espèгe en parler demain avec mes


blog coquin September 24, 2014 at 3:08 pm

Il me tardе de lire le prochain post


partouze hard October 15, 2014 at 2:32 am

Voսs nous concoctez continuellement des postes ρassionnants


fétichisme gratuit December 6, 2014 at 2:02 pm

Je sսis pressé de liree le prochain рoste


locks and keys January 30, 2015 at 3:31 am

At this time I am ready to do my breakfast, after having my breakfast
coming over again to read more news.


lesbienne January 24, 2016 at 11:08 am

C’est unn vrai plaisir de visiter votre post


Bisa Nomerix February 2, 2016 at 9:47 pm

I wrote down your blog in my bookmark. I hope that it somehow did not fall and continues to be a great place for reading texts.


amatrice cougar February 4, 2016 at 11:29 am

Je suis clairement du même avis que toi

Reply February 6, 2016 at 11:06 am

Est-ilpossible de emprunter deux trois lignes pour mmon site internet personnel ?


milf chaude February 6, 2016 at 11:06 am

Un article plein de conseils


bonasse mature February 6, 2016 at 11:35 am

Vous écrivez toujours des articles fascinants


mamans chaudasse February 6, 2016 at 11:41 am

On voit immédiatement que vous maîtrisez superbement bien ce sujet


o March 4, 2016 at 5:21 pm

C’est bizarre je pensais écrire un petit article semblable à celui là


Leave a Comment

{ 1 trackback }

Previous post:

Next post: