Wordpress - How to include PHP files in plugins the correct way

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.


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

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!