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
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
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.
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/?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/?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.
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
.
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 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
wp-load.php
? If you're testing, you don't need to create atest.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 ♦ Commented Dec 17, 2018 at 0:20get_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 Commented Dec 18, 2018 at 2:15home.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 ♦ Commented Dec 18, 2018 at 2:30wp_cron
. I appreciate your efforts. But the question is clear and valid. A down vote is a witch hunt. – Toskan Commented Dec 18, 2018 at 2:34get_header
from a different framework, why didn't you ask that? – Tom J Nowell ♦ Commented Dec 18, 2018 at 2:37