简体   繁体   中英

Wordpress plugin and action hooks

I'm new in WP and I'm trying to create a new plugin and I'm having problems when adding new actions defined inside a class. I guess the root problem is the way the code is executing and the init action hook but I cannot find my answer on internet and I want to know if there is an alternatve for this:

plugin.php file

...
require_once( PLUGIN_DIR . 'class.plugin.php' );
add_action( 'init', array( 'Plugin', 'init' ) );
...

class.plugin.php

class Plugin {

private static $instance = false;

public static function init() {

   if ( !self::$instance ) {
        self::$instance = true;
        self::init_hooks();           
    }
} 

private static function init_hooks() {
    add_action('init','foo1');
    add_filter('filter1', 'foo2');
}
public static function foo1(){
   ....
}

public static function foo2($var) {

  ....
}

You need to change your action hook with

private static function init_hooks() {
   add_action('init',array( 'Plugin', 'foo1' ) );
   add_filter('filter1', array( 'Plugin', 'foo2' ));

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