0

I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).

the function inside the plugins is like this

if( !function_exists('send-invoice') ){
 function send-invoice(){
  //The Plugin Invoice
 }
}

This is what I did

function send-invoice(){
 //My Custom Invoice
}
add_action('init', 'send-invoice');

How can I make sure that my code runs before the plugin codes? The plugin load before the theme, I tried plugin-loaded hook but nothing changed

Yaya Adam
  • 25
  • 3
  • The reason for the `if (!function_exists(...` is because the programmer had no idea if it already existed, and if it didn’t, then create it. Well, it obviously already existed. So when you redeclared it (tried to create it again)... it complained. – Tim Morton Dec 02 '20 at 06:20
  • no you can't. what is the original function trying to do anyway, you could prolly do a workaround as an alternative – Kevin Dec 02 '20 at 06:23
  • according to the plugin creator, they wrapped it with if (!function_exists()) so the user can override it with their own. For now i've been informed that the plugin load before the theme, so I have to make sure that my code load before plugin. I tried differnt hooks, made sure it load before the plug-in but it's not effective – Yaya Adam Dec 02 '20 at 06:56

2 Answers2

1

You can to use the anonymous function for example:

add_action('init', function() {
   //code here
});

More detail is here

Or use another hook muplugins_loaded:

function send-invoice(){
 //My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • The function is called in other files. I need to override that function. If I use anonymous function I have to modify the rest of the plugin – Yaya Adam Dec 02 '20 at 07:01
  • Thank you, I've tried that hook already but still no effect. I assume it's because of the fact that the snippet plugin hasn't been loaded yet – Yaya Adam Dec 02 '20 at 09:24
  • @YayaAdam also you can to see here - https://stackoverflow.com/questions/3620659/is-it-possible-to-overwrite-a-function-in-php – Dmitry Leiko Dec 02 '20 at 11:47
0

If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.

The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.

The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way

Diego
  • 1,610
  • 1
  • 14
  • 26
  • The function is inside a file `invoice.php`, and that file is called from the main plugin file `include_once(local_path . '/functions/invoice.php');` – Yaya Adam Dec 02 '20 at 09:22
  • is the include wrapped inside any function? does the plugin file has a class? or it's just old style PHP? Anyway, you could try with the dirty and quick method first, then eventually look for the cleanest (if possibile) – Diego Dec 02 '20 at 09:24
  • it's not wrapped in any function, all `include-once()` are right after ` – Yaya Adam Dec 02 '20 at 09:26
  • I guess I have to do as you mentioned, creating a new plugin – Yaya Adam Dec 02 '20 at 09:28
  • If it is not wrapped in anything then yeah, you are forced for a new plugin. Maybe not named with "aaa" but still a new plugin. Let us know, and if this work, mark as accepted please – Diego Dec 02 '20 at 09:57