admin管理员组文章数量:1130349
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
Share Improve this question edited Dec 16, 2018 at 23:47 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 16, 2018 at 22:05 ToskanToskan 5242 gold badges8 silver badges23 bronze badges 13 | Show 8 more comments1 Answer
Reset to default 0Note 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/?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.
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
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
Share Improve this question edited Dec 16, 2018 at 23:47 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 16, 2018 at 22:05 ToskanToskan 5242 gold badges8 silver badges23 bronze badges 13-
Why do you need to load
wp-load.php? If you're testing, you don't need to create atest.phpand 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: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 Commented 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 ♦ Commented 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 Commented 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_headerfrom a different framework, why didn't you ask that? – Tom J Nowell ♦ Commented Dec 18, 2018 at 2:37
1 Answer
Reset to default 0Note 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/?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.
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
本文标签: How to include wploadphp and have all plugins load as well
版权声明:本文标题:How to include wp-load.php and have all plugins load as well 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749089147a2314245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp-load.php? If you're testing, you don't need to create atest.phpand 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_headerfrom a different framework, why didn't you ask that? – Tom J Nowell ♦ Commented Dec 18, 2018 at 2:37