WordPress plugin: how to enable "Calling the 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 a database table to Excel. Basically, I have two files in my plugin, I send an email request to the download.php file with the name of the table to be exported. I want to be sure of the download.php file that is registered by the administrator, but when I call any of the WordPress Core functions, a fatal error occurs. I include wp-load.php and other 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 () work fine, without errors. But I want to upload this plugin to www.wordpress.org.

They said that including files using this method is not very good, and we cannot approve of it because the file structures in another WordPress installation may be different, in which case it will not work.

Here is the answer from wordpress.org -

In download.php, which STILL is not protected in a 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 just about any other main WordPress file that you must invoke directly via include is not a good idea and we cannot approve of the plugin. who does this if he has no good reason to download the file (s). It is crashing because not all WordPress installations have the same file structure.

Usually plugins will include wp-config.php or wp-load.php to access the core WordPress features, but there are much better ways to do this. Best of all, if you bind your processing functions (those that need, but do not have access to the main functions) to an action, for example, "init" or "admin_init".

For more information, see the plugins API reference: http://codex.wordpress.org/Plugin_API

For other features or for a better understanding of why we do not allow this, read the following: http://ottopress.com/2010/dont-include-wp-load-please/

If you are trying to use it because you need to access WordPress features outside of WordPress, we would really prefer you not to. Your plugin should be located inside WordPress, available only to people who are logged in and authorized if such access is required. Your plugin pages should be called through the control panel, like all other settings panels, and thus they will always have access to WordPress features.

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

+2
source share
1 answer

Include the download.php file after downloading the WP files. Here is a sample code to put in the main file of the plugin (or loader):

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

You can also use other actions, such as admin_init , if you need to download this file earlier. You can also specify the path to the file using plugin_dir_path(__FILE__) , this will return the path to your plugin directory, and you can use it as follows:

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

this can be useful if your download.php file is located in a subfolder (e.g. inc /). In this case, your inclusion function might look like this:

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

0
source

All Articles