Schlagwort: register_styles

WordPress is_page() reloaded

29. November 2012 - wordpress

Um Nebenwirkungen gerade auf komplexen Webseiten auszuschliessen sollte man sich daran erinnern, Code und  Styles nur dort einzufügen, wo sie wirklich benötigt werden.

English summary: This is just a reminder for me (and maybe you?) not to flood whole websites with scripts and css. By using the old and familar is_page() we can inject our code and styles only into those pages that definitely need them.

Im Beispiel sollen Styles und Scripts nicht auf den Seiten home, page1 und page2 inkludiert werden

class abc {
....
function abc() {
// if (is_page(....) doesn't work here. At this point, no information about pages is available.

add_action('wp_print_scripts', array ('abc','my_scripts'));
add_action('wp_print_styles', array ('abc', 'my_styles'));
}
function my_scripts() {
{
if  (is_page( array( 'page1','page2','home') )) return;
wp_register_script('script1', scriptname, dependencies,false,true);
wp_enqueue_script('script1');
}

function my_styles() {
if  (is_page( array( 'page1','page2','home') )) return;
wp_register_style('style1',stylename);
wp_enqueue_style('style1');
}
....