I'm building a plugin using the WordPress Plugin Boilerplate by DevinVinson.
Everything works fine, except one thing:
I'm trying to enqueue scripts and css only when a shortcode is present in the page.
In define_public_hooks
function, I added a call to the loader to register scripts instead of enqueuing them:
private function define_public_hooks() {
// ...
$this->loader->add_action( 'wp_register_script', $plugin_public, 'register_styles' );
$this->loader->add_action( 'wp_register_script', $plugin_public, 'register_scripts' );
// ...
}
Then in class-my-plugin-public.php
file I created two public functions register_styles()
and register_scripts()
. Inside these functions, I just register scripts and styles, instead of enqueuing them. It's something basic, like this:
public function register_scripts() {
wp_register_script( $this->plugin_name . '_google_maps_api', '//maps.googleapis.com/maps/api/js?key='. $gmap_key .'&v=3&libraries=places', array( 'jquery' ), '3', true );
}
Then I would like to enqueue them in the shortcode return function, but they do not get registered at all.
I checked with wp_script_is( $this->plugin_name . '_google_maps_api', 'registered' )
, and it returns false
.
If I make the very same stuff, but with enqueue
instead of register
everything works fine.
Any idea why?