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?

    you're doing something wrong if you get that message. Make sure you include any files before you start running functions from these files

    thats no it, the calls are within the files i'm including!

    lol, now I see `WP_PLUGIN_URL` in your code above :)

    Put very simply you can only include() files via a filepath and not a URI.

    This Codex article (probably written after you asked your question) is quite helpful:

    Are you doing this in a PHP file that's being used as an AJAX endpoint, or a form handler? You should **never** make direct calls to PHP files inside WordPress themes or plugins. Also including URLs doesn't work, if it did you'd have a massive security problem, and terrible performance

  • Bainternet

    Bainternet Correct answer

    10 years ago

    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:


    i read about at the WordPress support. and again thanks for answering!

    Can you please reconsider to mark this answer ( as accepted one?

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM