-1

Why load wp-load.php 'use wordpress ajax instead'

no

This is for testing only. I have a test page I use for local development only, e.g. test.php in my themes folder

Now when I do this:

global $wpdb;
require_once(__DIR__ . '/../../../wp-load.php');
get_header();
...
get_footer();

I notice that the plugin hooks are not executed, all my scripts and css arent loaded

is there something that I can do, so the plugin hooks load as well?

I tried as well to include my plugin file manually, but doesn't work.

E.g.

require_once(__DIR__ . '/../../plugins/myplugin-plugin/myplugin-plugin.php');

doesn't solve the issue

13
  • Why do you need to load wp-load.php? If you're testing, you don't need to create a test.php and load it directly in the browser, there are far better ways, easier ways of doing these things. Case in point you've ran into all these problems, there's a reason people are telling you to use Admin AJAX, or the superior REST API. What is it that you're testing?
    – Tom J Nowell
    Dec 17, 2018 at 0:20
  • @TomJNowell to have access to the wordpress functions like get_header(). You know, i'm upset, it's in my question, i'm down voted for no reason. You recommend rest api, and ajax. Both are no proper solution to the access to that function
    – Toskan
    Dec 18, 2018 at 2:15
  • You provided no context, are you trying to test theme templates? If so, is a page template not an appropriate solution that avoids this? Or a virtual page via rewrite rules? A home.php? Fundamentally what you're asking for is usually done by people who don't know about Admin AJAX or how to handle forms properly. Unless you say why you're trying to do this, people will assume that, after all you're behaving exactly the same as somebody who's stubborn and wants to use it for AJAX. There are better ways to test template functions
    – Tom J Nowell
    Dec 18, 2018 at 2:30
  • @TomJNowell well maybe that would work too, but that wasn't my question. Maybe I don't want to add a db entry for a test page into my app but just I file I can simply add and remove and be done with it? maybe I want to call from a different framework, outside of the wordpress world, maybe I want to use a cron that is not the buggy wp_cron. I appreciate your efforts. But the question is clear and valid. A down vote is a witch hunt.
    – Toskan
    Dec 18, 2018 at 2:34
  • You don't need to add a DB entry, not that it would incur any kind of cost. It would have been better to ask for a solution to your problem of testing template functions in a lightweight and easy manner. Instead, you devised a solution, but it didn't work so you asked for a solution to your solution to your problem, aka an XY problem. Eitherway it sounds like you have a very specific use case, but you're hiding it from us for some reason. If you wanted to call get_header from a different framework, why didn't you ask that?
    – Tom J Nowell
    Dec 18, 2018 at 2:37

1 Answer 1

0

Note that simply bootstrapping directly for a one off temporary file is never the optimal solution, and bad practice, and tends to introduce lots of problems. E.g. in your case, you might have WordPress functions available, but no theme files or APIs are triggered.

Instead, there are better ways to do it. This isn't considered bad practice without reason.

In a Browser

If you want to mess around with templates, and don't want to create page templates, you can put this in your functions.php:

add_filter( 'template_include', function($template) {
    return isset($_GET['test']) ? locate_template(['test.php']) : $template ;
}, 99 );

Now, when you visit yoursite.com/?test, it will load the test.php template in your theme.

You could even expand this further:

add_filter( 'template_include', function($template) {
    return !empty($_GET['test']) ? locate_template(['test/'.$_GET['test'].'.php') : $template ;
}, 99 );

Now you can place any PHP file inside a test sub folder in your theme, and access it via yoursite.com/?test=foo to load test/foo.php. As a bonus, you no longer need to count how many folders up wp-load.php is, and it will work on any WP install no matter how the folders are configured

Just don't do this on a live server.

In CLI

Additionally, if you have WP CLI installed, you can run wp eval test.php to do this from the command line.

You can also run wp shell to get an interactive PHP shell with WP functions loaded, and to test cron jobs, you can run wp cron test.

In a Cron Job, or Another Template

You can run get_template_part('test') to load the test.php template from somewhere else in WP.

You can also use wp cron event run --due-now to trigger WP Cron via an actual cron job. There's no need to bootstrap WordPress from a PHP file to get reliable cron in WordPress.

In Automated Testing

In general, you would not test templates this way. A proper test would either create posts, or mock posts. This is usually done by spawning a test environment, and pre-filling it with a database of known content, and running tests, reverting the database to the known good example in the process.

Doing this allows for integration tests using tools such as Behat which run automated browser tests by spawning headless browsers such as phantom, or even firefox instances.

Alternatively, for testing classes, you could use standard PHPUnit, combined with mocks for the WP APIs used.

Keep in mind that most major production themes on enterprise sites have some sort of tests subfolder that gets ran by phpunit or some other CI tool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.