Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

thanks for any help!

share|improve this question
    
you're doing something wrong if you get that message. Make sure you include any files before you start running functions from these files –  onetrickpony Jan 21 '11 at 2:55
    
thats no it, the calls are within the files i'm including! –  Bainternet Jan 21 '11 at 3:06
    
lol, now I see WP_PLUGIN_URL in your code above :) –  onetrickpony Jan 21 '11 at 4:04
3  
Put very simply you can only include() files via a filepath and not a URI. –  editor Jan 21 '11 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 –  henrywright Mar 12 at 10:52
add comment

6 Answers

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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";
share|improve this answer
add comment

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

share|improve this answer
    
As the @mtekk say I would recomend you to use tis structure: require_once(dirname(FILE) . '/filename.php); –  Webord Jan 21 '11 at 3:35
    
thanks i got it! –  Bainternet Jan 21 '11 at 4:02
add comment

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?

share|improve this answer
    
thanks i got it –  Bainternet Jan 21 '11 at 4:01
add comment
up vote 0 down vote accepted

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!

share|improve this answer
    
Lol i know what worked –  Bainternet Jan 23 '11 at 6:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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