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
+8-5Lines changed: 8 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,16 +8,19 @@ This is a matter of preference and judgement, but these registrations are super
8
8
9
9
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
10
11
-
###### Additionally, always register assets globally. Never scope a script registration inside `is_admin()` or another check to prevent registration collision.
11
+
###### Always register assets globally
12
12
13
-
#### Store dependency handles in accessible variables and constants
13
+
Never scope a script registration inside `is_admin()` or another check to prevent registration collision.
14
14
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.
15
+
#### Store the string used to register/enqueue an asset in a static class variable or PHP constant
16
+
17
+
This allows the entire class and application interact programmatically with the asset, preventing string scavenger hunts across files and easing rename.
16
18
17
19
#### Naming dependency handle slugs
18
20
***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
+
***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`)
22
+
***Try to keep parity between filename and dependency string**. This makes life easier and applications scale nicer.
23
+
***Plan for growth: avoid calling dependency "my-product.ext."** Use `-primary`, `-core` or `-main` prefix.
21
24
* Never use WordPress Filters or difficult-to-predict dynamic variables to register asset handles so others may dequeue and register handles with confidence.
22
25
23
26
#### Leverage dependency chaining and the `array()` method for `wp_enqueue_*()`
0 commit comments