How to include PHP files in plugins the correct way

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?

Solutions

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

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.

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!

Similar questions

The proper way to include/require PHP files in WordPress
I'm a new WordPress developer and recently I've been having problems (on multiple sites) with include_once and require_once for PHP files. If I include (get_theme_directory_uri() . 'subdir/file') the specified file gets included (or required, which leads to fatal errors) but if any WordPress functions are called within 'file' I get something simila...
Proper way to include PHP files in Wordpress theme
I am trying to develop a Wordpress theme which requires a lot of PHP functions and custom classes. I am wondering the best practice for including additional PHP files within Worpdress. Example: Do I list every file to include using: When I review other developers themes, I never stumble across a batch of "include() statements, so I believe I must b...
Is there a way of putting the facebook social plugins into page template php files?
I was wondering if there was any way of putting the features that come with the "Facebook" wordpress plugin, such as comments, recommend and subscribe, on my front page which I created using a template page, like maybe a do_shortcode or get_shortcode solution to put it into the page? Thanks :)
What is the correct way to use WordPress functions outside WordPress files?
I read about 2 methods for initializing WordPress function outside of WordPress files so We can use these functions on any page or website outside the WordPress blog. Which one of these 2 methods is the correct one? What are the use cases for each method if both are correct? What is the deference between using one method or the other? Method 1: Met...
What is the correct way to enqueue multiple CSS files?
I am building a Bootstrap site on WordPress and need to be able include multiple stylesheets. However, when I enqueue them as follows, only the first and third style sheets show up in the page source. I have confirmed that all three files are on the server. What am I missing?
Correct way to template files for WooCommerce
I've been building a template for WooCommerce and all is well, but I'm getting at the point for making the product template and getting confused. I read all about using the WooCommerce template structure and copying that into my own theme folder. BUT I just created single-product.php without copying that woo commerce folder in my theme folder and I...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.