WordPress register scripts and enqueue automatically

Connect on Social

I have an FYI for you, more for myself but if this info helps others, double-bonus.

I have recently started combining my love for AngularJS with my experience on ongoing work with WordPress. The thing about working with AngularJS is there are a lot of scripts needed for even a small project, goes with the territory.  Before you start thinking “Why aren’t you combining and minifying your scripts?”, I know. It is on my list, Things I need to know more, understanding and using more tools like GULP and Grunt.

Until I find the answer to magically creating more time in my day and I know all there is to AngularJS, I need to enqueue all my scripts in WordPress. The latest project is creating a login page with AngularJS, connecting to an Office365 SharePoint list and pulling data back to WordPress. There is only a login page with the login form, and then the file display and client data is loaded and with that, there is six core AngularJS script files, five service scripts and two controllers. The goal here is only to load these when needed, no big deal right. Just use something like is_page() and load only what you need. My preferred process is the register everything and enqueue when you need them. I didn’t like the idea of registering and writing all that and started wondering if wp_register_script() would take an array of registered script handles to enqueue. I stumbled across the following in the codex. Function Reference/wp register script

Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued. WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script’s handle as a dependency.

Awesome as script dependency is used all over AngularJS. The core file is the dependency for the other core files, all the services and modules are dependencies and included in your base module, usually referred to as “app”. Then the controllers depend on those, so you can register all your scripts and then enqueue 2 and they are all called. Here is my list and layout.

Read More

7 Comments

  1. Good day! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward
    to new updates.

  2. Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t
    show up. Grrrr… well I’m not writing all that over again.
    Anyway, just wanted to say great blog!

    1. No, not really, all my comments are vetted before being seen on the site. Just to make sure everyone is playing nice. I appreciate your comments, thanks for taking the time to post.

  3. Hurrah, that’s the things i was looking for, what a
    information! present at this website, thanks admin on this
    site.

Leave a Reply to Corey Upfield Cancel reply

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