Changing Your WordPress Theme from an External PHP Script

For a recent Press Up project, I wanted to change the running theme on a WordPress installation adjacent to some other PHP code that was doing some work. I knew it had to be possible, and I was even pleasantly surprised to find out how easy it was. It illustrates two novel things I’d not realized about WordPress before, so let’s dive in.

Tying into the functionality of a working WordPress installation

wordpress-usb-stick-ready-to-go

WordPress is ready to, if you can just find the right thing to plug in

I knew the first thing I would need to do is pull in WordPress functionality. What I didn’t quite anticipate was that this would only require one line of code. I knew it had to be pretty simple because WordPress uses the front controller design pattern, and so had to be “bootstrapping” — loading up — its functionality somewhere. I started to dig through the tree at index.php, but then took Fred’s advice and did a quick Google search which revealed the best answer:

include('path/to/wpsite/wp-load.php');

What wp-load.php does seems so obvious I maybe shouldn’t even bother to tell you: it loads WordPress. This is called in the “bootstrap” or “sunrise” process when WordPress is going to render a whole page for your site that is requested from the front controller. This is called when you site is responding to an AJAX request. This is called from every single admin-side “page controller”.

What calling it does is load up all those functions you can use in WordPress without thinking. Maybe you really want to use wp_list_pluck() outside of WordPress. Maybe you want to change the theme of that WordPress site. Whatever your reason, if you want to use a bit of WordPress without being inside of a WordPress context including() or requiring() the file wp-load.php is your solution.

Settting the Theme

Well we’ve got WordPress loaded, now we can just call WordPress functions exactly as we would within a WordPress theme or plugin. And before I needed to do it, I figured that changing the theme may be a bit complicated. But again a few quick searches and I found the switch_theme() function. It’s dead simple and did exactly what I needed, though I wasn’t clear what the Codex was saying about it. (I’ve since edited the Codex for clarity — I should do this more, and so should anyone else who learns a detail the Codex isn’t clear on. But that’s a topic for another time…) To use it where all the WordPress functions have been loaded, you just need a line like:

switch_theme('folder-slug-of-theme');

It really couldn’t be much simpler if it tried. A well-named function with an obvious signature. C’est manifique!

Tying it all together

It’s easy to forget about how great the size, scale, and community around something like WordPress can be. It’s great to be reminded from time to time.

If you’re a skimmer or just find it valuable to see the whole thing together, here’s how you set the running theme on a WordPress site from an outside PHP script:

<?php 
include('path/to/wpsite/wp-load.php');
switch_theme('folder-slug-of-theme');

If you execute a script with just those two lines it, from a command line invocation, a browser page load, or elsewhere, you’ll probably get an error. You obviously need to change both lines to correspond to exactly what you’re doing. But otherwise it’ll work fine.

This was one of the quickest and most straight-forward problem-solving experiences I’ve had coding with WordPress in a while. What could have been a hairy problem that involved a custom database query and a deep-dive into WordPress and dissecting all its bits was instead quick and painless.

There are perks to using a popular, and commonly-studied and talked about CMS like WordPress: usually the internet has solved your problem. It’s easy to forget about how great the size, scale, and community around something like WordPress can be. It’s great to be reminded from time to time.

Image credit: Alex Barth, Debs


3 Responses

Comments

  • Thomas Piccirello says:

    I too just discovered this about two weeks ago and was amazed at how easy it is to tie in WordPress’s core functionality to a custom php page. It’s a way nicer solution that the alternative, which is to create a custom page template that loads said PHP file (though that does have benefits, such as easily adding the page to your menu).

    The WordPress function library is impressively large, so there’s so much you can once you’ve loaded it. For my use case I didn’t have to switch themes, though I also wasn’t aware of the function that allowed you to do so. Thanks!

Pingbacks

  • […] on top of this basic layer of functions in the specified language that is available. I just wrote a little tutorial on WPShout about the switch_theme() function that WordPress makes available. The way I interact and work with this is decided by the WordPress […]