Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Load Scripts on Plugin Pages Only

Posted on May 10, 2011 by Pippin in Intermediate, Quick Tips, Tutorials, Working with jQuery, Writing Plugins 3 Comments
Home» Tutorials » Intermediate » Load Scripts on Plugin Pages Only
Tweet
Love It - 1

Loading scripts, CSS and jQuery, within a plugin for use on admin pages is one of the most common areas that developers make mistakes. If you plugin’s administrative pages require any scripts that are not already loaded in WordPress by default, you should always ensure that those scripts are only loaded on your plugin’s page, and not on every other page inside of the WordPress dashboard.

Limiting your scripts loads to specific pages is really pretty simple.

1
2
3
4
5
6
7
8
9
10
11
12
function your_plugin_admin_styles() 
{
        // this is a variable that holds the path to your plugin folder, defined outside of this function
	global $plugin_base_dir;	
	wp_enqueue_style('your-plugin-admin', $plugin_base_dir . 'includes/css/admin-styles.css');
}
// load the scripts on only the plugin admin page 
if (isset($_GET['page']) && ($_GET['page'] == 'your-plugin-folder/your-plugin-file.php')) 
{ 
        // if we are on the plugin page, enable the script
	add_action('admin_print_styles', your_plugin_admin_styles');
}

I have given you a simple example enqueuing only one CSS file, but you can easily add as many scripts as you need.

Loading your scripts in this manner is very important because it helps to mitigate the possibility of conflicts with other plugins. It also helps to increase page loading times within the dashboard. If each admin page has to load the scripts for every plugin active, you are going to experience much delayed loading time.

Tweet Follow @pippinsplugins
CSS, jquery, Scripts

3 comments on “Load Scripts on Plugin Pages Only”

  1. Mark says:
    May 16, 2011 at 8:17 pm

    This awesome! I’ve been having trouble with this recently. Is adding a script to the front end via a plugin such as nivoslider done in a similar way? I tried a myriad of tuts on that subject and it all ended in failure.

    Thanks!

    Reply
    • pippin says:
      May 16, 2011 at 8:25 pm

      Yes, this is almost exactly how it would be done. The only difference is the hook that is used. Instead of add_action(‘admin_print_scripts’, your_plugin_admin_styles’);, you would use add_action(‘wp_print_scripts’, your_plugin_admin_styles’);

  2. diljan says:
    May 2, 2013 at 5:40 pm

    hi pippin

    you forgott one litle thing on line 11 :-)

    instead of:
    add_action(‘admin_print_styles’, your_plugin_admin_styles’);

    change to:
    add_action(‘admin_print_styles’, ‘your_plugin_admin_styles’);

    the ” ‘ ” in the action, before ” your_plugin_admin_styles’); “

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Plugin Thoughts Episode 7 – Remember Core CSS
  • Use wp_localize_script, It Is Awesome
  • Create a Live Search in WordPress with jQuery and Ajax
  • Loading Scripts Correctly in the WordPress Admin
  • Review: Cudazi Scroll to Top

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (4)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • I was just interviewed by @WP_Squared. Check it out: http://t.co/aWtKOlKVfb
    May 23, 2013
  • @no_fear_inc Cool, will hope to see you there!
    May 23, 2013
  • @no_fear_inc Are you coming?
    May 23, 2013

Topics

contextual help featured get_user_meta attachments campaign monitor register_setting wp_enqueue_script hook Rémi Corson the_content shortcodes Sugar Event Calendar add_shortcode mail chimp plugin authors attachment image forms login short codes do_action Related posts comments recent posts post types apply_filters bbpress short code taxonomies custom post type images gallery Ajax Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins