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?

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.

Leave a Comment