My problem is when on the main plugin file I include a PHP file something like this:
include(WP_PLUGIN_URL . '/wordpress-group-buying/ipn/paypal-ipn.php');
// or
include_once(WP_PLUGIN_URL . '/wordpress-group-buying/ipn/paypal-ipn.php');
// or
require(WP_PLUGIN_URL . '/wordpress-group-buying/ipn/paypal-ipn.php');
// or
require_once(WP_PLUGIN_URL . '/wordpress-group-buying/ipn/paypal-ipn.php');
and on that file I have a call to a WordPress function like:
add_action('hook', 'callback');
and I get:
Fatal Error: Call to undefined function add_action()
Now before you say “use if(**function_exists**('add_action')){
” if I use that then it just doesn’t work.
The questions:
- What would be the correct way to do that?
- What are the difference between
include
,include_once
,require
and when do I use witch?
Coming in late to this party, but here’s the “WordPress” way: use
plugin_dir_path( __FILE__ )
, e.g.:Note that the function does return the trailing slash for the filepath.
I looked through a couple of plugins that I previously created to see the diferent ways that I have included extra files inside of plugins and I noticed there are two methods you can use, there are probably more.
Define your plugin directory
Inside of your plugin have the following definition to define the current plugin location.
Example code:
Just a straight up include or require
You can simply use; include, include_once, require or require_once inside of your plugin folder by referencing the location like in the below example code. The below example will be based on a file in your root plugin directory including another file from within a folder inside of your plugin folder.
Example code:
I end up forgoing the WordPress constructs for includes and use the following:
I don’t think it will actually solve your issue, which seems to be a scope issue, but it is the code I use.
As for the difference between include and require:
include will throw a warning if the file is not found
require will throw a fatal error if the file is not found
include_once and require_once will not include/require the file/code again if it has already been included/required (note that as far as I can tell, this is only for a specific file in a specific directory).
The info above is from the PHP documentation, the thing is there is not a correct one, will depend on the need of the code, I do require() on important stuff like functions, but on theme files like footer or the loop I use include_once or include because i can handle the warning and say to the user/visitor that happend an error instead of just a fatal_error
First , thank you to everyone who answered,
My problem was calling the included files with full url that way they don’t go through WordPress. and that happened because as i stated on the question i was calling them from the main plugin file. so the fix ended up using:
i read about at the WordPress support.
and again thanks for answering!
or
or
Note : to enqueu .css & .js files
admin_enqueue_scripts
inside plugin useplugin_dir_url( __FILE__ )
Hi @×× ××ת ×תר××:
When WordPress is loading it defines the
add_action()
function before it attempts to load any plugins The fact you are getting the error tells me you are doing something strange or that something is wrong with your WordPress install.Who are you getting your “plugin” to load? Are you using an
include*()
orrequire*()
to load it, maybe in yourwp-config.php
file?Whenever you create a new file inside your working directory, you have to include it everytime. But try a method to scan your directroy and attach it automatically, not only the php files, Which helps to include php, js and css fiules properly on the both sides( backend, front end).
http://kvcodes.com/2014/05/wordpress-theme-development-include-files-automatically/
It may also work:
include WP_PLUGIN_DIR . '/plugin-name/ipn/paypal-ipn.php';