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

Plugin Development 101 – Intro to Loading Scripts and Styles

Posted on March 14, 2013 by Pippin in Member Restricted, Subscriber Only, Tutorials, Working with jQuery, Writing Plugins 16 Comments
Home» Member Restricted » Plugin Development 101 – Intro to Loading Scripts and Styles
wordpress-plugin-development_101
Tweet
Love It - 5
This entry is part 7 of 8 in the Plugin Development 101 Series
← Plugin Development 101 – Intro to Short CodesPlugin Development 101 – Introduction to Adding Dashboard Menus →
  • Introduction to WordPress Plugin Development 101
  • Plugin Development 101 – What Makes a Plugin?
  • Plugin Development 101 – General Best Practices
  • Plugin Development 101 – An Intro to Filters
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • Plugin Development 101 – Introduction to Adding Dashboard Menus

In this part of Plugin Development 101 you will be introduced to how to load scripts and styles in your WordPress plugins. This is a fundamental element of WordPress plugin development and is used in the vast majority of all plugins. There are many ways that asset loading can be done wrong, so I want to ensure that you learn early how to do it correctly.

The code written in the video is shown below:

1
2
3
4
5
function pd101_load_styles() {
	wp_enqueue_style( 'pd101-styles', plugins_url( 'pd101-styles.css', __FILE__ ) );
	wp_enqueue_script( 'pd101-scripts', plugins_url( 'pd101-scripts.js', __FILE__ ), array( 'jquery' ), '1.0' );
}
add_action( 'wp_enqueue_scripts', 'pd101_load_styles' );

Codex reference for the functions used:

  • wp_enqueue_script()
  • wp_enqueue_style()
  • plugins_url()
  • wp_enqueue_scripts (action)
Tweet Follow @pippinsplugins

16 comments on “Plugin Development 101 – Intro to Loading Scripts and Styles”

  1. Edward Caissie says:
    March 15, 2013 at 7:23 am

    Although they appear similar, is there a benefit to using your method versus the following:

    wp_enqueue_script( 'pd101-scripts', plugin_dir_url( __FILE__ ) . 'pd101-scripts.js', array( 'jquery' ), '1.0' );

    Reply
    • Pippin says:
      March 15, 2013 at 8:52 am

      In regards to the URL?

    • Edward Caissie says:
      March 15, 2013 at 9:02 am

      Yes, specifically:
      plugins_url( 'pd101-scripts.js', __FILE__ ) versus plugin_dir_url( __FILE__ ) . 'pd101-scripts.js' … just wondering if there was a benefit, or is it a preference?

      I prefer the `plugin_dir_url` method for readability sake but if there is a benefit to your method then I’d look at switching over to it for my plugins.

      PS: Is it me, or is this comment text a bit light when writing?

    • Pippin says:
      March 15, 2013 at 10:22 am

      Really no difference.

  2. MonkeyMays says:
    March 15, 2013 at 8:54 pm

    I recently asked a guy on Twitter – who was giving WP answers – if I should enqueue CSS and scripts in a plugin, and got a stupid answer….. So, this post hits the spot!

    Thanks, Pippin! It’s like you read my mind…!!

    Reply
    • Pippin says:
      March 16, 2013 at 12:31 pm

      I’m curious to know what the answer was he gave. Care yo share? No need to disclose his name.

  3. MonkeyMays says:
    March 16, 2013 at 5:31 pm

    Really I got two…
    First he said “Generally not. One way to optimize this is to use a caching plugin. Generally I use WP Super Cache for quick setup.”

    Completely confused with that answer, I realize he must have misunderstood the question, so I clarified “oh, I meant when developing a plugin for the repository, I know this is standard for themes.”

    Then the second stupid answer: “Not really a coder sorry. More of a developer that helps WordPress beginners”

    Call me old school, but I’ve never heard of a developer who doesn’t code.

    Reply
    • Pippin says:
      March 17, 2013 at 9:54 am

      Sounds like a “Web Design Shop” that doesn’t know basic CSS :D

  4. designerken says:
    March 22, 2013 at 2:12 pm

    What about when you put your assets in sub folders? (ie. css and js folders)

    would it then be: plugins_url( 'js/pd101-scripts.js', __FILE__ )

    Reply
    • Pippin says:
      March 22, 2013 at 4:34 pm

      Yep!

  5. Barrett Golding says:
    April 13, 2013 at 8:51 am

    for sites w/ frequent changes to their stylesheet, any downside in forcing a refresh by using the file mod-time as version number? eg:
    http://www.mojowill.com/developer/get-your-wordpress-css-changes-noticed-immediately/

    Reply
    • Pippin says:
      April 13, 2013 at 9:58 am

      Nope, that’s a great idea!

  6. sayed taqui says:
    May 6, 2013 at 1:49 pm

    Wonderful tutorial learned how version number can help in update, never knew that before. Just for curosity, in themes we have to enque several stylesheets and scripts(specially in case of sliders), is there any short way to enque multiple scripts or stylesheets? Or do we have to create seperate functions for each of them and then hook them individually?

    Reply
    • Pippin says:
      May 6, 2013 at 3:08 pm

      You can place all of the wp_enqueue_script/style() call into a single function that is hooked to “wp_enqueue_scripts”. Here’s an example: https://easydigitaldownloads.com/codex/source-function-edd_load_scripts.html#15-75

  7. sayed taqui says:
    May 6, 2013 at 3:26 pm

    Thanks, one function and one time “add_action” that saves time and space. http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts made me think that we have to do it individually because they did it twice, probably I misunderstood. Thanks for clearifying.
    And I became a premimum member but I still cannot see an option to ask question in your forum. Is there something else I have to do?

    Reply
    • Pippin says:
      May 6, 2013 at 5:28 pm

      Ask a question in my support forum, or a general plugin-dev related question?

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

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 (2)

    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

  • RT @wpdailyco: Shop Front - A Free WP Theme for Easy Digital Downloads http://t.co/vJPGfh1DKo
    May 21, 2013
  • @jchristopher No problem
    May 21, 2013
  • @wpmodder hmmm, haven&#039;t seem that. Feel free to open a ticket if you want help
    May 21, 2013

Topics

campaign monitor shortcodes Rémi Corson add_options_page the_content Sugar Event Calendar featured get_user_meta contextual help add_shortcode attachments Tom McFarlin wp_enqueue_script short codes mail chimp authors Related posts attachment plugin image login forms do_action recent posts post types bbpress apply_filters comments short code taxonomies custom post type images Ajax gallery Stripe taxonomy jquery widgets users 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) 2011 Pippin's Plugins