WordPress Plugin: How to resolve “Call to undefine

2019-05-18 10:07发布

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. 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');

In this case, functions like auth_redirect() working fine without any error. But I want to upload this plugin to 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.

Here is the reply from wordpress.org-

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');

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.

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

Please consult the Plugins API reference for more information: 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/

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.

This is my very first WordPress plugin and after many hours of struggling and reading all of these, I do not have solution.

1条回答
地球回转人心会变
2楼-- · 2019-05-18 10:14

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');
}

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');
}

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')

查看更多
登录 后发表回答