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,requireand when do I use witch?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Coming in late to this party, but here’s the “WordPress” way: use plugin_dir_path( __FILE__ ), e.g.:
<?php include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php'); ?>
Note that the function does return the trailing slash for the filepath.
Method 2
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:
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
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:
include "classes/plugin-core.php";
Method 3
I end up forgoing the WordPress constructs for includes and use the following:
require_once(dirname(__FILE__) . '/filename.php);
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).
Method 4
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:
include_once('/ipn/paypal-ipn.php');
i read about at the WordPress support.
and again thanks for answering!
Method 5
Include
The include() statement includes and evaluates the specified file.
Include Once
The include_once() statement includes and evaluates the specified
file during the execution of the
script. This is a behavior similar to
the include() statement, with the only
difference being that if the code from
a file has already been included, it
will not be included again. As the
name suggests, it will be included
just once.Require
require() and include() are identical in every way except how they
handle failure. They both produce a
Warning, but require() results in a
Fatal Error. In other words, don’t
hesitate to use require() if you want
a missing file to halt processing of
the page.Require Once
The require_once() statement includes and evaluates the specified
file during the execution of the
script. This is a behavior similar to
the require() statement, with the only
difference being that if the code from
a file has already been included, it
will not be included again.
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
Method 6
include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php');
or
define( 'PLUGIN_ROOT_DIR', plugin_dir_path( __FILE__ ) ); include( PLUGIN_ROOT_DIR . 'ipn/paypal-ipn.php');
or
$plugin_dir_path = plugin_dir_path( __FILE__ ); include( $plugin_dir_path . 'ipn/paypal-ipn.php');
Note : to enqueu .css & .js files admin_enqueue_scripts inside plugin use plugin_dir_url( __FILE__ )
$plugin_dir_uri = plugin_dir_url( __FILE__ ); wp_enqueue_style( 'plugin-style', $plugin_dir_uri . 'css/plugin-style.css');
Method 7
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*() or require*() to load it, maybe in your wp-config.php file?
Method 8
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/
Method 9
It may also work:
include WP_PLUGIN_DIR . '/plugin-name/ipn/paypal-ipn.php';
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0