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:

Read More
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?

Related posts

Leave a Reply

9 comments

  1. 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";
    
  2. 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).

  3. 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

  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!

  5. 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');
    
  6. 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?