WordPress插件:如何解决“对未定义函数add_action()和add_action()的调用"? [英] WordPress Plugin: How to resolve "Call to undefined function add_action() as well as add_action()"?

查看:319
本文介绍了WordPress插件:如何解决“对未定义函数add_action()和add_action()的调用"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在创建一个插件,用于将数据库表导出到Excel工作表.主要是我的插件中有两个文件,我正在发送一个后发请求,要求下载具有表名的download.php文件.我想确保已登录admin的download.php文件,但是当我调用任何WordPress Core函数时,会发生致命错误.我包括wp-load.php和其他类似的文件-

I am creating a plugin to export database table to excel sheet. 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. 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. I am including wp-load.php and others files like this -

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

在这种情况下,auth_redirect()之类的功能可以正常工作而不会出现任何错误.但是我想将此插件上传到www.wordpress.org.

In this case, functions like auth_redirect() working fine without any error. But I want to upload this plugin to www.wordpress.org.

他们说使用这种方法包含文件不是很好,我们不能批准它,因为另一个WordPress安装中的文件结构可能会有所不同,在这种情况下将无法正常工作.

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.org-

在download.php中,它仍然不受此方式的保护

In download.php, which is STILL not protected by the way

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

包括wp-config.php,wp-blog-header.php,wp-load.php或您必须直接通过include调用的几乎所有其他WordPress核心文件不是一个好主意,我们无法批准除非有充分的理由来加载文件,否则插件将这样做.由于并非所有WordPress安装都具有完全相同的文件结构,因此很容易失败.

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). It is prone to failure since not all WordPress installs have the exact same file structure.

通常,插件将包括wp-config.php或wp-load.php以获得对WordPress核心功能的访问权限,但是有更好的方法来执行此操作.最好将处理功能(需要但无法访问核心功能的功能)绑定到动作挂钩中,例如"init"或"admin_init".

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. 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".

有关更多信息,请查阅Plugins API参考: http://codex.wordpress.org/Plugin_API

Please consult the Plugins API reference for more information: http://codex.wordpress.org/Plugin_API

对于其他可能性,或者为了更好地理解我们为什么不允许这样做,请阅读以下内容: http://ottopress.com/2010/dont-include-wp-load-please/

For other possibilities, or to better understand why we disallow this, read this: http://ottopress.com/2010/dont-include-wp-load-please/

如果您由于需要访问WordPress之外的WordPress功能而尝试使用它,那么实际上我们宁愿您根本不这样做.您的插件应该在WordPress内,如果需要这种访问权限,则只有已登录并获得授权的人员才能访问.像所有其他设置面板一样,应通过仪表板调用您插件的页面,这样,它们将始终可以访问WordPress功能.

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. Your plugin should be inside WordPress, only accessible to people who are logged in and authorized, if it needs that kind of access. 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.

推荐答案

在加载WP文件后,请包含download.php文件.这是放置在插件主文件(或加载程序)中的代码示例:

Include your download.php file after WP files are loaded. 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');
}

如果需要更早加载此文件,还可以使用其他操作,例如admin_init. 您还可以使用plugin_dir_path(__FILE__)指定文件的路径,这将返回插件目录的路径,您可以像这样使用它:

You can also use other actions like admin_init if you need this file to be loaded earlier. 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:

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

如果您的download.php位于子文件夹(例如inc/)中,这可能会变得很方便.在这种情况下,您的include函数可能如下所示:

this may become handy if your download.php located in subfolder (e.g. inc/). In this case your include function may look like this:

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

这篇关于WordPress插件:如何解决“对未定义函数add_action()和add_action()的调用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆