1
0
Fork 0
mirror of https://github.com/inretio/WordPress-Plugin-Boilerplate synced 2024-12-23 04:13:53 +02:00

Restricted scripts and styles to load only on plugin settings page if it is enabled.

If the Settings page has been activated, the scripts and styles will only be loaded on the settings page. If not, they will be loaded for all admin pages (like before this commit)

Also added the __FILE__ magic constant as a second parameter to the admin_enqueue_style and admin_enqueue_script, the user no longer needs to change 'plugin-name'. This also makes the code more robust.
This commit is contained in:
Mikkel Breum 2013-04-07 23:14:44 +02:00
parent fd69c019ec
commit 3d85de0c95

View file

@ -46,6 +46,9 @@ class PluginName {
# add_action('admin_menu', array( $this, 'plugin_admin_menu' ) ); # add_action('admin_menu', array( $this, 'plugin_admin_menu' ) );
// Register admin styles and scripts // Register admin styles and scripts
// If the Settings page has been activated (above), the scripts and styles
// will only be loaded on the settings page. If not, they will be loaded for all
// admin pages.
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
@ -119,18 +122,53 @@ class PluginName {
*/ */
public function register_admin_styles() { public function register_admin_styles() {
// TODO: Change 'plugin-name' to the name of your plugin /*
wp_enqueue_style( 'plugin-name-admin-styles', plugins_url( 'plugin-name/css/admin.css' ) ); * Check if the plugin has registered a settings page
* and if it has, make sure only to enqueue the scripts on the relevant screens
*/
if ( isset($this->plugin_screen_slug) ){
/*
* Check if current screen is the admin page for this plugin
* Don't enqueue script/style if it's not
*/
$screen = get_current_screen();
if ( $screen->id != $this->plugin_screen_slug )
return;
}
wp_enqueue_style( 'plugin-name-admin-styles', plugins_url( 'css/admin.css', __FILE__ ) );
} // end register_admin_styles } // end register_admin_styles
/** /**
* Registers and enqueues admin-specific JavaScript. * Registers and enqueues admin-specific JavaScript.
*/ */
public function register_admin_scripts() { public function register_admin_scripts() {
// TODO: Change 'plugin-name' to the name of your plugin /*
wp_enqueue_script( 'plugin-name-admin-script', plugins_url( 'plugin-name/js/admin.js' ), array('jquery') ); * Check if the plugin has registered a settings page
* and if it has, make sure only to enqueue the scripts on the relevant screens
*/
if ( isset($this->plugin_screen_slug) ){
/*
* Check if current screen is the admin page for this plugin
* Don't enqueue script/style if it's not
*/
$screen = get_current_screen();
if ( $screen->id != $this->plugin_screen_slug )
return;
}
wp_enqueue_script( 'plugin-name-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array('jquery') );
} // end register_admin_scripts } // end register_admin_scripts
@ -172,6 +210,7 @@ class PluginName {
} // end plugin_admin_page } // end plugin_admin_page
/*--------------------------------------------* /*--------------------------------------------*
* Core Functions * Core Functions
*---------------------------------------------*/ *---------------------------------------------*/