1

Ok I am trying to modify some code in a WP Plugin. The plugin declares a file for this:

// overwrite hooks 
$plugin_dir = trailingslashit(dirname(dirname(__FILE__)));
if(file_exists($plugin_dir . "auto_overwrite.php")){
include_once($plugin_dir . "auto_overwrite.php");
}

So I created a file in the above directory called auto_overwrite.php and included the function I wanted to change.

The problem I am having is that I still get the error:

Fatal error: Cannot redeclare listing_tabs() (previously declared in....

I thought the idea of include_once was that is would not include functions that are already defined.

The other function is in a file named meta_boxes.php and it too is called via

include_once("meta_boxes.php");

I am relatively new to PHP so maybe I'm missing something, any help is greatly appreciated. Thanks!

EDIT: From answers below I see that I cant just call out the function as include_once only limits that specific file from being loaded more than once. Basically I am trying to modify a function specified downstream from this file. Is it possible to do this?

Shane
  • 25
  • 7
  • `include_once` will only prevent a file from being included more than once. – DiddleDot Dec 28 '16 at 17:53
  • Possible duplicate of [In PHP, how do I check if a function exists?](http://stackoverflow.com/questions/4351835/in-php-how-do-i-check-if-a-function-exists) – Devon Dec 28 '16 at 17:54
  • Functions can't be declared more than once, check the link above for how you can wrap a declaration. – Devon Dec 28 '16 at 17:55
  • I think I get it now. My function will be called before the plugin so is there anyway to overwrite is without editing the plugin file? I tried duplicating the entire file (with modifications) and calling that inside the auto_overwrite.php file using include_once( get_stylesheet_directory() . 'meta_boxes.php'); but – Shane Dec 28 '16 at 18:14
  • I get the same error. – Shane Dec 28 '16 at 18:15

1 Answers1

0

You can check if a function has been defined, and if not act accordingly like this...

 if (!function_exists('listing_tabs')){
    // include file with function
 }
Duane Lortie
  • 1,281
  • 1
  • 12
  • 16