4

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' ) );
2
  • yes it's possible and you did it right, did you get any error?
    – GKS
    Dec 6 '16 at 4:43
  • Hi @GovindKumar was not getting any error, however the scripts are not enqueued when I view the source of the page. I did use wp_enqueue_script function I just forgot to include it on my example. sorry. that class LocatorMap is being hooked to the register_activation_hook function as include_once( 'inc/class-locator-map.php' ); register_activation_hook( __FILE__, array( 'LocatorMap', 'init' ) ); Dec 6 '16 at 5:05
14

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' );
1
  • 1
    ahh yeah, I only need to register the scripts once. so I kinda use that register_activation_hook can I use the add_action( 'init', array( 'LocatorMap', 'init' ) ); ? but init maybe do it too. I think? Dec 6 '16 at 22:16
3

use the function get_called_class()

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

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());
0

I recently had to do the same thing and ended up using the static:: late static binding along with special ::class constant. The static:: binding will reference the calling class, while the ::class constant will return a string that is the name of the calling class (with namespacing!)

Using the example from the questiom, the implementation would like something like this:

class LocatorMap {
    
    public static function init() {
        add_action( 'wp_enqueue_scripts', array( static::class, 'registerScripts' ) );
    }

    public function registerScripts() {}
}

Since static:: references the calling class, this allows to write reusable classes for your plugins/themes, and kind of wrap up some of WordPress' legacy ugliness.

For example, you could write an abstract class for custom post types like this:


namespace WordpressPlugin;

abstract class AbstractPostType {
  const SLUG;

  private function __construct() {}

  public static function initialize() {
    add_action( 'init', array( static::class, 'registerPostType' ), 0 );
    add_action( 'init', array( static::class, 'registerTaxonomies' ), 1 );
    add_action( 'add_meta_boxes', array( static::class, 'addMetaBox' ) );
  }

  public static function registerPostType() {}

  public static function registerTaxonomies() {}

  public static function addMetaBox() {}
}

Aftet that, you can now create custom post types without having to duplicate all of the boilerplate in each subclass, like this:

namespace WordpressPlugin;

class ExamplePostType extends AbstractPostType {
  const SLUG = 'example-post-type';

  public static function registerPostType() {
    register_post_type( self::SLUG, array(
      ...
    ) );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.