I'm creating a plugin that always enqueues a script in the footer. I also have a function that if called would enqueue an additional script that depends on the other one. Here is the basic format of my plugin...
<?php
class MyPlugin {
public function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
}
public function enqueue_scripts() {
wp_enqueue_script('myscript', plugins_url('js/myscript.js', __FILE__), array('jquery'), null, true);
}
public function api() {
add_action('wp_enqueue_scripts', array($this, 'api_scripts'));
}
public function api_scripts() {
wp_enqueue_script('api', plugins_url('js/api.js', __FILE__), array('myscript'), null, true);
}
}
$_GLOBALS['myplugin'] = new MyPlugin();
In my theme's functions.php file I would like to know when I can make a call to enqueue the script. I've tried attaching to plugins_loaded, but the script is not called.
<?php
function myfunction() {
global $myplugin;
$myplugin->api();
}
add_action('plugins_loaded', 'myfunction');