From 68da31e4be70b286b785b454d36eea80120f84f8 Mon Sep 17 00:00:00 2001 From: Mikkel Breum Date: Sun, 7 Apr 2013 22:05:15 +0200 Subject: [PATCH 1/4] correct action hook for register_admin_styles Changed the action hook from 'admin_print_styles' to 'admin_enqueue_scripts' which is better. See: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts See: http://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/ --- plugin-boilerplate/plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-boilerplate/plugin.php b/plugin-boilerplate/plugin.php index 75c9fd8..990d549 100644 --- a/plugin-boilerplate/plugin.php +++ b/plugin-boilerplate/plugin.php @@ -42,7 +42,7 @@ class PluginName { add_action( 'init', array( $this, 'plugin_textdomain' ) ); // Register admin styles and scripts - add_action( 'admin_print_styles', 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' ) ); // Register site styles and scripts From 403ef654ea1b8ad9f8f7addb3287e6498d4f4e4a Mon Sep 17 00:00:00 2001 From: Mikkel Breum Date: Sun, 7 Apr 2013 22:56:08 +0200 Subject: [PATCH 2/4] Removed the need to customize url for wp_enqueue_style and wp_enqueue_scripts By adding the __FILE__ magic constant as a second parameter to the wp_enqueue_style and wp_enqueue_scripts, the user no longer needs to change 'plugin-name'. This also makes the code more robust. --- plugin-boilerplate/plugin.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin-boilerplate/plugin.php b/plugin-boilerplate/plugin.php index 990d549..d5cc755 100644 --- a/plugin-boilerplate/plugin.php +++ b/plugin-boilerplate/plugin.php @@ -135,8 +135,7 @@ class PluginName { */ public function register_plugin_styles() { - // TODO: Change 'plugin-name' to the name of your plugin - wp_enqueue_style( 'plugin-name-plugin-styles', plugins_url( 'plugin-name/css/display.css' ) ); + wp_enqueue_style( 'plugin-name-plugin-styles', plugins_url( 'css/display.css', __FILE__ ) ); } // end register_plugin_styles @@ -145,8 +144,7 @@ class PluginName { */ public function register_plugin_scripts() { - // TODO: Change 'plugin-name' to the name of your plugin - wp_enqueue_script( 'plugin-name-plugin-script', plugins_url( 'plugin-name/js/display.js' ), array('jquery') ); + wp_enqueue_script( 'plugin-name-plugin-script', plugins_url( 'js/display.js', __FILE__ ), array('jquery') ); } // end register_plugin_scripts From fd69c019ec7aa374581b49384cf32578be22e7f8 Mon Sep 17 00:00:00 2001 From: Mikkel Breum Date: Sun, 7 Apr 2013 23:05:13 +0200 Subject: [PATCH 3/4] Added Plugin Settings page (commented out) Added a Plugin Settings page, that can be enabled by uncommenting the line that hooks it into the admin_menu action hook. Added additional views that contain the WordPress standard markup to wrap the plugin settings markup. The views are now loaded into the plugin page. --- plugin-boilerplate/plugin.php | 24 ++++++++++++++++++++++++ plugin-boilerplate/views/admin_close.php | 1 + plugin-boilerplate/views/admin_open.php | 3 +++ 3 files changed, 28 insertions(+) create mode 100644 plugin-boilerplate/views/admin_close.php create mode 100644 plugin-boilerplate/views/admin_open.php diff --git a/plugin-boilerplate/plugin.php b/plugin-boilerplate/plugin.php index d5cc755..59c0cd9 100644 --- a/plugin-boilerplate/plugin.php +++ b/plugin-boilerplate/plugin.php @@ -41,6 +41,10 @@ class PluginName { // Load plugin text domain add_action( 'init', array( $this, 'plugin_textdomain' ) ); + // Add the options page and menu item + // Uncomment the following line to enable the Settings Page for the plugin + # add_action('admin_menu', array( $this, 'plugin_admin_menu' ) ); + // Register admin styles and scripts add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) ); @@ -148,6 +152,26 @@ class PluginName { } // end register_plugin_scripts + + function plugin_admin_menu() { + // TODO: Change 'Page Title' to the title of your plugin admin page + // TODO: Change 'Menu Text' to the text for menu item for the plugin settings page + // TODO: Change 'plugin-name' to the name of your plugin + $this->plugin_screen_slug = add_plugins_page('Page Title', 'Menu Text', 'read', 'plugin-name', array( $this, 'plugin_admin_page' )); + + } // end plugin_admin_menu + + + function plugin_admin_page() { + + // output the cpanel + include_once('views/admin_open.php'); + include_once('views/admin.php'); // outputs the settings/form markup for the plugin admin markup + include_once('views/admin_close.php'); + + } // end plugin_admin_page + + /*--------------------------------------------* * Core Functions *---------------------------------------------*/ diff --git a/plugin-boilerplate/views/admin_close.php b/plugin-boilerplate/views/admin_close.php new file mode 100644 index 0000000..e2d7a0a --- /dev/null +++ b/plugin-boilerplate/views/admin_close.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/plugin-boilerplate/views/admin_open.php b/plugin-boilerplate/views/admin_open.php new file mode 100644 index 0000000..db7549e --- /dev/null +++ b/plugin-boilerplate/views/admin_open.php @@ -0,0 +1,3 @@ +
+ +

My Plugin Name

From 3d85de0c957c614daa95d78cb4111a44afde2b41 Mon Sep 17 00:00:00 2001 From: Mikkel Breum Date: Sun, 7 Apr 2013 23:14:44 +0200 Subject: [PATCH 4/4] 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. --- plugin-boilerplate/plugin.php | 47 ++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/plugin-boilerplate/plugin.php b/plugin-boilerplate/plugin.php index 59c0cd9..402dade 100644 --- a/plugin-boilerplate/plugin.php +++ b/plugin-boilerplate/plugin.php @@ -46,6 +46,9 @@ class PluginName { # add_action('admin_menu', array( $this, 'plugin_admin_menu' ) ); // 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_scripts' ) ); @@ -119,18 +122,53 @@ class PluginName { */ 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 + /** * Registers and enqueues admin-specific JavaScript. */ 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 @@ -172,6 +210,7 @@ class PluginName { } // end plugin_admin_page + /*--------------------------------------------* * Core Functions *---------------------------------------------*/