80

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?
6
  • you're doing something wrong if you get that message. Make sure you include any files before you start running functions from these files Jan 21, 2011 at 2:55
  • thats no it, the calls are within the files i'm including!
    – Bainternet
    Jan 21, 2011 at 3:06
  • lol, now I see WP_PLUGIN_URL in your code above :) Jan 21, 2011 at 4:04
  • 3
    Put very simply you can only include() files via a filepath and not a URI.
    – editor
    Jan 21, 2011 at 5:25
  • 1
    This Codex article (probably written after you asked your question) is quite helpful: codex.wordpress.org/Determining_Plugin_and_Content_Directories Mar 12, 2014 at 10:52

9 Answers 9

107

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.

2
  • 4
    Note that by using __FILE__ it will output relative to the current file you call it from, so if your include statement is done from a subdirectory inside your plugin file structure you'll get the subdirectory back too. Jan 30, 2018 at 20:30
  • 2
    The alternative - if you NEED relative paths, is require_once(plugin_dir_path(__DIR__).'/myfile.inc');
    – FoggyDay
    Oct 4, 2019 at 20:28
10

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";
1
  • relative includes can bring all kinds of nasty unexpected issues. Dec 1, 2017 at 7:47
6

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

0
6

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

1
  • As the @mtekk say I would recomend you to use tis structure: require_once(dirname(FILE) . '/filename.php);
    – Webord
    Jan 21, 2011 at 3:35
6

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!

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

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?

0
0

Whenever you create a new file inside your working directory, you have to include it everytime. But try a method to scan your directroy and attach it automatically, not only the php files, Which helps to include php, js and css fiules properly on the both sides( backend, front end).

http://kvcodes.com/2014/05/wordpress-theme-development-include-files-automatically/

1
  • 1
    Please add relevant information from the link provided to your answer. Use the link for credit purposes. Please edit your question May 29, 2014 at 5:48
0

It may also work:

include WP_PLUGIN_DIR . '/plugin-name/ipn/paypal-ipn.php';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.