質問

So basically, what I'm trying to do is to hook a static method of a class to another static method of that same class.

Code is here :

class LocatorMap {

public static function init() {

    add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );

}

/* add_action( 'wp_enqueue_script', array( 'LocatorMap', 'register_scripts' ) ); */
public function register_scripts() {

    global $post;

    /* http or https */
    $scheme = parse_url( get_bloginfo('url'), PHP_URL_SCHEME );

    /* register gmaps api and info box */
    wp_register_script( 'google-maps-api', $scheme . '://maps.googleapis.com/maps/api/js', array('jquery'), FALSE, true );
    wp_register_script( 'google-maps-info-box', $scheme . '://cdn.rawgit.com/googlemaps/v3-utility-library/infobox/1.1.13/src/infobox.js', array( 'jquery', 'google-maps-api' ), '1.1.13', true ); 

}

}

Is this possible? I don't know since I'm a bit new on this kind of a structure.

UPDATE I am also calling this class on an external file.

define( DEALERSHIP_MAP_URL, untrailingslashit( plugin_dir_url( __FILE__ ) )  );
define( DEALERSHIP_MAP_DIR, untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( DEALERSHIP_MAP_TEMPLATE, DEALERSHIP_MAP_DIR . '/templates' );

require_once( 'core/class-locator-map.php' );

register_activation_hook( __FILE__, array( 'LocatorMap', 'init' ) );
役に立ちましたか?

解決

register_activation_hook only runs once i.e. when the plugin is first activated - use the init hook instead to "boot up" your plugin:

add_action( 'init', 'LocatorMap::init' );

他のヒント

use the function get_called_class()

public static function init() {
    add_action( 'wp_enqueue_scripts', array( get_called_class(), 'register_scripts' ) );
}

Use Array For Statice Method:-

Example 1:-

add_action('init',  array('Say', 'Hello'));

class Say{
    public static function Hello(){
        echo 34;   
    }
}

Example 2:-

add_action('init',   Say::Hello());
ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top