How to Use WordPress Functions in a Non-WordPress Custom PHP File – WPQuickies

In this lunchtime #WPQuickies, I show you how to use WordPress functions inside non-WordPress custom PHP files.

There are many use-cases where you may need to create a custom PHP script outside of the WordPress system.

You may be generating PDF template files, or dealing with files generated from a 3rd-party system.

How are you then able to access WordPress functions like get_user_by() or get_the_title() inside your custom PHP scripts?

Stand-Alone PHP Files

What do I mean by non-WordPress PHP files?

In this context, I’m talking about PHP files that are not associated with the WordPress core, plugins or themes.

These PHP files could be part of another external system or app that is being hosted in the same area as your WordPress website.

It could be a single PHP file that you are running as a server cronjob or a complex app that you use as part of your business.

What About The REST API?

Yes you can use the WordPress REST API to get and post information to a WordPress website without the need to use PHP functions but this tutorial is about getting access to the core WordPress PHP functions. 

What Are WordPress Functions?

When working with a WordPress website, developers are used to being able to call WordPress functions to get information about the content WordPress stores.

This content could be users, products, posts, navigation menus and anything in between.

As long as you are working within the WordPress application environment, most of the core, plugin and theme functions are available for your to use.

However, once you leave that environment, those functions are no longer available – they are termed out-of-context or out-of-scope.

Loading or “Bootstrapping” WordPress

In order to get access to those native PHP WordPress functions in your external PHP file, you will need to load or “bootstrap” WordPress so that your application is in scope.

How Does WordPress Load?

Like every other PHP-based web application, it all starts in the index.php file and WordPress is no different.

If we look at the default WordPress index.php file we can see it only does a couple of things.

 <?php
 /**
  * Front to the WordPress application. This file doesn't do anything, but loads
  * wp-blog-header.php which does and tells WordPress to load the theme.
  *
  * @package WordPress
  */
 /**
  * Tells WordPress to load the WordPress theme and output it.
  *
  * @var bool
  */
 define( 'WP_USE_THEMES', true );
 /** Loads the WordPress Environment and Template */
 require __DIR__ . '/wp-blog-header.php'; 

This file defines the variable WP_USE_THEMES to true and loads in the file wp-blog-header.php file for the same directory on the server.

If we look at the wp-blog-header.php file.

 <?php
 /**
  * Loads the WordPress environment and template.
  *
  * @package WordPress
  */
 if ( ! isset( $wp_did_header ) ) {
 $wp_did_header = true;
 // Load the WordPress library.
 require_once __DIR__ . '/wp-load.php';
 // Set up the WordPress query.
 wp();
 // Load the theme template.
 require_once ABSPATH . WPINC . '/template-loader.php';
 } 

The main purpose of this file is to load in the file wp-load.php file which starts the process of loading the WordPress application and all its core files into memory, giving us the context to call those functions.

Which Files Do You Need To Call To Bootstrap WordPress?

Now we know there are two possible files we need to load in to make WordPress functions available to your stand-alone PHP file; wp-blog-header.php and wp-load.php

Because wp-blog-header.php essentially just loads in wp-load.php we can use the latter to bootstrap WordPress.

Do You Need To Access Theme Functions?

The line define( ‘WP_USE_THEMES’, true ); in the wp-blog-header.php file tells WordPress whether or not to load the theme template files which is the normal WordPress bootstrap process.

If you just need access to WordPress core functions, you can set this value to false and save some loading time and memory.

Loading WordPress In Your External Custom PHP File

So here is the correct way of loading or bootstrapping WordPress in a stand-alone external custom PHP file.

Before you call any WordPress functions, include these two lines in your PHP file.

 define( 'WP_USE_THEMES', false );
 require_once( $_SERVER[ 'DOCUMENT_ROOT' ] . '/wp-load.php' ); 

You can set WP_USE_THEMES to true if you need to load in the current theme’s template files.

The command $_SERVER[ ‘DOCUMENT_ROOT ‘] returns the file server path to the root of where the webserver files are expected to be.

If you have installed WordPress in a subdirectory, you will need to add that subdirectory name before the wp-load.php file.

For example if WordPress is installed in a “wordpress” subdirectory the line would change to:

require_once( $_SERVER[ 'DOCUMENT_ROOT' ] . '/wordpress/wp-load.php' );

#WPQuickies

Join me every Thursday at 1 pm Sydney time for some more WPQuickies – WordPress tips and tricks in thirty minutes or less.

Broadcasting live on YouTube and Facebook.

Suggest a #WPQuickies Topic

If you have an WordPress topic you’d like to see explained in 30 mins or under, fill out the form below.

https://forms.gle/mMWCNd3L2cyDFBA57

Looking to level up your WordPress development skills?

WordPress Plugin Development book

I self-taught myself WordPress development, but this book took it to another level.

Each chapter builds upon the next, but you can also use it to jump into specific areas like WP Cron, WP REST endpoints, building a custom plugin repository etc.

If you’re like me and like having reference books at hand, this is the one that sits on my desk all the time.

Was this article helpful?
YesNo

4 Responses

  1. Hi, I think your blog might be having browser
    compatibility issues. When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, fantastic blog!