Skip to content

Commit b96b732

Browse files
authored
Merge pull request #7 from WordPress-Phoenix/march-29-2018
March 29 2018
2 parents 8194670 + ceaee7a commit b96b732

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

docs/best-practices-asset-management.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# WordPress Asset Management Best Practices
22

3+
##### WordPress uses internal functions to register *(`wp_register_script()` & `wp_register_style()`)* and enqueue *(`wp_enqueue_script()` & `wp-enqueue_style()`)* static JavaScript and CSS files so that WordPress Core, Themes and Plugins can **programatically interact with scripts and styles.**
4+
5+
This approach allows WordPress to:
6+
1. Load registered dependencies
7+
1. Chose the printing order & location of files in the DOM
8+
1. Print inline script/style alongside referenced files
9+
1. Print inline js variables to make PHP data safely available to the script via the Window variable (`wp_localize_script()` -- originally used for "localizing" translated strings from WordPress language packs into in labels needed in scripts).
10+
311
#### Break `wp_register_script()` and `wp_register_style()` arguments onto their own lines
412

513
This is a matter of preference and judgement, but these registrations are super important, often overrun 80 characters and are worth breaking out to multiple lines for easy locating in files and nice code hygene.
@@ -8,17 +16,20 @@ This is a matter of preference and judgement, but these registrations are super
816

917
Always register CSS and JavaScript first instead of directly enqueueing them. This makes dequeueing easier for other products interacting with your dependency in the environment.
1018

11-
###### Additionally, always register assets globally. Never scope a script registration inside `is_admin()` or another check to prevent registration collision.
19+
#### Always register assets globally
1220

13-
#### Store dependency handles in accessible variables and constants
21+
Never scope a script registration inside `is_admin()` or another check to prevent registration collision. Only scope enqueues.
1422

15-
When registering CSS or JS within a PHP class, always store the asset handle as a class variable. When registering a dependency outside a class, define a PHP constant. This allows the entire class and application interact programmatically with the asset, preventing string scavenger hunts across files and easing rename.
23+
#### Store the string used to register/enqueue an asset in a static class variable or PHP constant
24+
25+
This allows the entire class and application interact programmatically with the asset, preventing string scavenger hunts across files and easing rename.
1626

1727
#### Naming dependency handle slugs
1828
* **This is a global namespace**. Be creative, be courteous, be concise, be clear and be defensive.
19-
* Please don't describe the asset with a postfix like `-js`, `-css`, `-script`, `-style` -- WordPress will add postfixes when printing assets in the DOM, resulting in `something-js-js`. However, if a product is called purecss or momentjs, we then use the full slug with repetitive postfix, despite repetion (i.e. `purecss-css`)
20-
* When appropriate, try to match the filename. This makes life easier and applications scale nicer.
21-
* Never use WordPress Filters or difficult-to-predict dynamic variables to register asset handles so others may dequeue and register handles with confidence.
29+
* **Please don't use a postfix like `-js`, `-css`, `-script`, `-style`** -- WordPress will add postfixes when printing assets in the DOM, resulting in `something-js-js`. However, if a product is called purecss or momentjs, we then use the full slug with repetitive postfix, despite repetion (i.e. `purecss-css`)
30+
* **Try to keep parity between filename and dependency string**. This makes life easier and applications scale nicer.
31+
* **Plan for growth: avoid calling dependency `my-product`** Use `-primary`, `-core` or `-main` postfix.
32+
* **Never use WordPress Filters or difficult-to-predict dynamic variables** so others may dequeue and register handles with confidence.
2233

2334
#### Leverage dependency chaining and the `array()` method for `wp_enqueue_*()`
2435

@@ -53,3 +64,6 @@ Plus at time of writing it's 2017 and React and Vue-based apps, use of JavaScrip
5364

5465
#### Provide a version string for caching
5566
We recommend tying product assets the version for the Theme or Plugin you're in. Having a condition that checks for local environments (i.e. check request string for ".test") and toggling between a production version and `time()` is another good option for cache-busting on a local env.
67+
68+
###### NEW: Working with Assets containing cache-hash
69+
Often when working with JavaScript SPA frameworks and tools, a unique hash is generated in the filename. To account for these assets

0 commit comments

Comments
 (0)