Intermediatenote 05 of 1603 reasons · 03 notes
WordPressIntermediate
Load assets only where needed
Conditionally enqueue heavy scripts so every page does not pay for one feature.
Code example
Copy, adapt, and ship.
Read the numbered lines, copy the complete snippet, then adjust the values for your project.
add_action( 'wp_enqueue_scripts', function () { if ( is_page_template( 'templates/calculator.php' ) ) { wp_enqueue_script( 'calculator', get_theme_file_uri( 'assets/calculator.js' ), [], '1.0', true ); }});Why this matters
The production reason.
- 01
A script needed on one landing page should not slow down the entire site.
- 02
Conditionally enqueue assets by page template, block presence, shortcode, or route.
- 03
This keeps Core Web Vitals healthier and reduces conflicts between unrelated scripts.
How it holds up outside a demo.
- WordPressUse is_page, is_singular, or has_block checks before enqueueing.
- WooCommerceLoad checkout helpers only on checkout routes.
- ElementorDisable unused widget assets where possible.
Explore other stacks
Jump into a different technology.
16 WordPress notes on this shelf, and six more stacks beside it.
Use next/image for image optimization
Next.js image optimization starts with next/image sizing, lazy loading, modern formats, and predictable dimensions.
Open the noteKeep state close to the interaction
Local state avoids unnecessary parent renders and keeps component intent obvious.
Open the noteAbort stale fetch requests
Cancel outdated browser requests so fast UI changes do not render old data.
Open the noteBuild a CSS gradient generator with code export
A CSS gradient generator should preview color stops, preserve accessible contrast, and export clean CSS code.
Open the noteReduce LCP with image optimization
Reduce LCP with image optimization by resizing, converting, and compressing images before they reach production.
Open the noteUse real buttons for actions
If it changes state, use button. If it navigates, use a link.
Open the note