我正在开发一个 WP 插件。
目前,我正在使用 jquery 对插件文件夹中的独立 php 文件进行 ajax 调用。
我们假设该文件名为 test.php。
该文件在此调用之前未加载,因此 native WordPress 函数在此文件中不起作用。
因此,我在 test.php 中有 require_once('wp.load');
。
这是我的问题:
我需要 WordPress 的唯一功能是 $wpdb->insert。有没有比 wp-load 更好的函数?不包括那么多的东西,因为我只需要这个功能!
我知道 require_once 会加载文件,仅当文件尚未加载时。也许我忽略了这种情况,但这如何与ajax一起工作呢?如果有“成功”响应,test.php 是否会关闭,因此当再次调用时,它会再次加载
wp-load.php
吗?
谢谢!
最佳答案
If there is a "success" response, does test.php close, thus when its called again, it loads wp-load.php again?
正是如此。背后的概念require_once
(或 include_once
)是为了确保您不会同时将相同的函数、类和变量重新加载到相同的当前运行的 PHP 代码中:
include_once
may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.
由于 PHP 脚本仅根据请求运行,因此您所描述的正是它会发生的情况。
也就是说,我不会过度考虑每次需要时加载整个 wp.load
的概念。我怀疑它会对性能造成如此明显的影响。
关于ajax - wp-load.php 和 require_once,在 ajax 调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551862/