Initial draft: Different ways of implementing on-demand loading in WordPress


Different ways of on-demand loading in WordPress (plugins, themes)
(or: WordPress: Different ways of implementing on-demand loading)

#1: Load in footer
- wp_register_script / style with @param $in_footer set to true
- wp_enqueue anywhere
=> use a helper function / wrapper:
function load_my_js() {

if( !wp_script_is( 'my_handle', 'enqueued' ) ) {
wp_enqueue( 'my_handle');
}
}
=> same with css
=> or just use a common wrapper, enqueuing both assets

#2: Use a trigger in the post
- shortcode
- data-* HTML attributes

#3: Use custom fields
- variation of #2 => requires the same techniques
- nicer GUI for the end-user thanks to ACF + similar plugins

#4: Use template
- semi-on demand, because it has be triggered by specific templates
-

#5: Lazy-loading via JS
- usage of wp_localize_script is recommended (global variables, eg. the URL to the JS you want to load)
- jQuery.getScript => JS
- or lazyload for CSS + JS => https://github.com/lewisje/lazyload (a more current fork)
- if specific selector (= trigger) found, post-load assets + initialize them (if applicable)

Quelle: Piraten-Planet
Autor:

Datum: Montag, 27. Juni 2016 14:16

Tags »