You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/best-practices-asset-management.md
+20-6Lines changed: 20 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,13 @@
1
1
# WordPress Asset Management Best Practices
2
2
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
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
+
3
11
#### Break `wp_register_script()` and `wp_register_style()` arguments onto their own lines
4
12
5
13
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
8
16
9
17
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.
10
18
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
12
20
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.
14
22
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.
16
26
17
27
#### Naming dependency handle slugs
18
28
***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.
22
33
23
34
#### Leverage dependency chaining and the `array()` method for `wp_enqueue_*()`
24
35
@@ -53,3 +64,6 @@ Plus at time of writing it's 2017 and React and Vue-based apps, use of JavaScrip
53
64
54
65
#### Provide a version string for caching
55
66
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