简体   繁体   中英

Override a plugin class

I'm trying to override a plugin class with my own.

The plugin class is

  function yith_woocompare_constructor() {
           // stuff here

 }
add_action( 'plugins_loaded', 'yith_woocompare_constructor' );

So in my functions.php I tried to do like that

     remove_action( 'plugins_loaded', 'yith_woocompare_constructor', 0 );   
// now add your own filter
add_action( 'plugins_loaded', 'new_yith_woocompare_constructor', 0 );

   function new_yith_woocompare_constructor()
    { 
      // my stuff here 
     }

But It doesn't seem to be working, what Am I doing wrong ?

The default priority is 10 but you're using 0. In your version you're not actually removing the action.

Also depending on the point your plugin loads it won't work anyway.

Try this instead.

function wpse_plugins_loaded() {
    remove_action( 'plugins_loaded', 'yith_woocompare_constructor' );   
    add_action( 'plugins_loaded', 'new_yith_woocompare_constructor' );
}
add_action( 'plugins_loaded', 'wpse_plugins_loaded', 5 );

function new_yith_woocompare_constructor() {

}

In this example we're waiting for all the plugins to load to remove the 'yith_woocompare_constructor' action. We're calling the remove_action on plugins loaded with a priority of 5. This means the action will be removed before it is run (on 10) then replacing it with the custom function.

you can do it by mu-plugins.

create directory name

mu-plugins parallel to plugins folder and in folder create file wp-plugin-extended.php , if your pluing name is wp-plugin.php and do the code

function yith_woocompare_constructor() {
           // stuff here

 }
add_action( 'plugins_loaded', 'yith_woocompare_constructor' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM