简体   繁体   English

WordPress插件:如何解决“对未定义函数add_action()和add_action()的调用”?

[英]WordPress Plugin: How to resolve “Call to undefined function add_action() as well as add_action()”?

Fatal error: Call to undefined function add_action() in D:\xampp\htdocs\excel\wp-content\plugins\DatabaseToExcel\download.php on line 13

I am creating a plugin to export database table to excel sheet. 我正在创建一个插件以将数据库表导出到Excel工作表。 Mainly I have two files in my plugin, I am sending a post request to download.php file with the table name which will be exported. 主要是我的插件中有两个文件,我正在发送一个后发请求,要求下载具有表名的download.php文件。 I want to be sure on download.php file that the admin is logged in, but when I am calling any of WordPress Core function, fatal error occurs. 我想确保已登录admin的download.php文件,但是当我调用任何WordPress Core函数时,发生致命错误。 I am including wp-load.php and others files like this - 我包括wp-load.php和其他类似的文件-

require_once('../../../wp-load.php');
require_once('../../../wp-config.php');
require_once('../../../wp-includes/load.php');

In this case, functions like auth_redirect() working fine without any error. 在这种情况下,auth_redirect()之类的功能可以正常工作而不会出现任何错误。 But I want to upload this plugin to www.wordpress.org. 但我想将此插件上传到www.wordpress.org。

They said that including files using this method is not good and we can not approve it because of file structures in another WordPress installation can vary and in that case it will not work. 他们说使用这种方法包含文件不是很好,我们不能批准它,因为另一个WordPress安装中的文件结构可能会有所不同,在这种情况下将无法使用。

Here is the reply from wordpress.org- 这是wordpress.org-的回复

In download.php, which is STILL not protected by the way 在download.php中,该方式仍然不受保护

require_once('../../../wp-load.php');
require_once('../../../wp-config.php');
require_once('../../../wp-includes/load.php');

Including wp-config.php, wp-blog-header.php, wp-load.php, or pretty much any other WordPress core file that you have to call directly via an include is not a good idea and we cannot approve a plugin that does so unless it has a very good reason to load the file(s). 包括wp-config.php,wp-blog-header.php,wp-load.php或几乎所有您必须直接通过include调用的其他WordPress核心文件都不是一个好主意,我们无法批准该插件除非有充分的理由来加载文件,否则请这样做。 It is prone to failure since not all WordPress installs have the exact same file structure. 由于并非所有WordPress安装都具有完全相同的文件结构,因此很容易失败。

Usually plugins will include wp-config.php or wp-load.php in order to gain access to core WordPress functions, but there are much better ways to do this. 通常,插件将包括wp-config.php或wp-load.php,以获取对WordPress核心功能的访问权限,但是有更好的方法来执行此操作。 It's best if you tie your processing functions (the ones that need but don't have access to core functions) into an action hook, such as "init" or "admin_init". 最好将处理功能(需要但无法访问核心功能的处理功能)绑定到动作挂钩中,例如“ init”或“ admin_init”。

Please consult the Plugins API reference for more information: http://codex.wordpress.org/Plugin_API 请参考插件API参考以获取更多信息: http : //codex.wordpress.org/Plugin_API

For other possibilities, or to better understand why we disallow this, read this: http://ottopress.com/2010/dont-include-wp-load-please/ 对于其他可能性,或者为了更好地理解我们为什么不允许这样做,请阅读以下内容: http : //ottopress.com/2010/dont-include-wp-load-please/

If you're trying to use it because you need to access WordPress functions outside of WordPress, we'd actually much rather you didn't do that at all. 如果您由于需要访问WordPress之外的WordPress功能而尝试使用它,那么实际上我们宁愿您根本不这样做。 Your plugin should be inside WordPress, only accessible to people who are logged in and authorized, if it needs that kind of access. 您的插件应位于WordPress内,如果需要这种访问权限,则只有已登录并获得授权的人员才能访问。 Your plugin's pages should be called via the dashboard like all the other settings panels, and in that way, they'll always have access to WordPress functions. 插件的页面应像其他所有设置面板一样通过仪表板调用,这样,它们将始终可以访问WordPress函数。

This is my very first WordPress plugin and after many hours of struggling and reading all of these, I do not have solution. 这是我的第一个WordPress插件,经过数小时的努力和阅读所有这些插件,我没有解决方案。

Include your download.php file after WP files are loaded. 加载WP文件后,包括您的download.php文件。 Here's example of code to place in your plugin's main file (or loader): 这是放置在插件主文件(或加载程序)中的代码示例:

add_action('wp_loaded', 'pluginPrefix_include_download_script');
function pluginPrefix_include_download_script() {
  require('download.php');
}

You can also use other actions like admin_init if you need this file to be loaded earlier. 如果您需要更早加载此文件,则还可以使用其他操作,例如admin_init You can also specify path to a file using plugin_dir_path(__FILE__) this will return path to your plugin directory and you can use it like this: 您还可以使用plugin_dir_path(__FILE__)指定文件的路径,这将返回您插件目录的路径,您可以像这样使用它:

function pluginPrefix_include_download_script() {
  $pluginDirPath = plugin_dir_path(__FILE__);
  require($pluginDirPath.'download.php');
}

this may become handy if your download.php located in subfolder (eg inc/). 如果您的download.php位于子文件夹(例如,inc /)中,则可能会变得很方便。 In this case your include function may look like this: 在这种情况下,您的include函数可能如下所示:

require($pluginDirPath.'inc/download.php')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM