Scriptloading
Um das Scriptloading nur auf einzelnen Seiten zu erlauben, kann man mit einer scriptloading.php arbeiten.
<?php
add_action( 'wp_print_scripts', 'deregister_cf7_javascript', 100 );
function deregister_cf7_javascript() {
if ( ! is_page( 19695 ) ) {
wp_deregister_script( 'wps-survey-jquery-js' );
} elseif ( ! is_page( array( 13, 17832, 19695, 20070 ) ) ) {
wp_deregister_script( 'contact-form-7' );
}
}
add_action( 'wp_print_styles', 'deregister_cf7_styles', 100 );
function deregister_cf7_styles() {
if ( ! is_page( 19695 ) ) {
wp_deregister_style( 'wps-survey-css' );
wp_deregister_style( 'wps-survey-override-css' );
} elseif ( ! is_page( array( 13, 17832, 19695, 20070 ) ) ) {
wp_deregister_style( 'contact-form-7' );
}
}
add_action( 'wp_print_scripts', 'deregister_vlb_javascript', 100 );
function deregister_vlb_javascript() {
if ( ! is_page( array( 9, 21, 15501, 22194 ) ) ) {
wp_deregister_script( 'video-lightbox' );
wp_deregister_script( 'jquery.prettyphoto' );
}
}
add_action( 'wp_print_styles', 'deregister_vlb_styles', 100 );
function deregister_vlb_styles() {
if ( ! is_page( array( 9, 21, 15501, 22194 ) ) ) {
wp_deregister_style( 'video-lightbox' );
wp_deregister_style( 'jquery.prettyphoto' );
}
}
add_action( 'wp_print_styles', 'deregister_pgnvi_styles', 100 );
function deregister_pgnvi_styles() {
if ( ! is_page( array( 3, 613 ) ) ) {
wp_deregister_style( 'wp-pagenavi' );
}
}
add_action( 'wp_print_styles', 'deregister_myml_styles', 100 );
function deregister_myml_styles() {
if ( ! is_page( 2183 ) ) {
wp_deregister_style( 'mymail-form' );
}
}
// Register some javascript files
add_action( 'wp_enqueue_scripts', 'register_isotope_files' );
function register_isotope_files() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_template_directory_uri() . '/asset/js/jquery-1.12.4.min.js', false );
wp_register_script( 'isotope', get_template_directory_uri() . '/asset/3rdparty/isotope-1.5/jquery.isotope.min.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'bbq', get_template_directory_uri() . '/asset/3rdparty/ba-bbq/jquery.ba-bbq.min.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'referenzen', get_template_directory_uri() . '/asset/js/referenzen.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'modernizr', get_template_directory_uri() . '/asset/3rdparty/modernizr-2.6.1-respond-1.1.0.min.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'fancy', get_template_directory_uri() . '/asset/js/jquery.fancybox.min.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'main', get_template_directory_uri() . '/asset/js/main.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'slick', get_template_directory_uri() . '/asset/js/jquery.carouselTicker.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'modernizr' );
wp_enqueue_script( 'isotope' );
wp_enqueue_script( 'bbq' );
wp_enqueue_script( 'referenzen' );
wp_enqueue_script( 'main' );
wp_enqueue_script( 'slick' );
if ( is_page( 25696 ) ) {
wp_enqueue_script( 'ajax-load-more' ); // Already registered, just needs to be enqueued
}
if ( is_page( array( 19695, 20671, 22059, 61, 22194, 28156 ) ) ) {
wp_enqueue_script( 'fancy' );
wp_enqueue_style( 'fancy-css', get_template_directory_uri() . '/asset/css/jquery.fancybox.min.css' );
wp_enqueue_style( 'owl-css', get_template_directory_uri() . '/asset/css/owl.carousel.min.css' );
}
$post = get_post();
if ( $post->post_parent == '26' ) {
wp_enqueue_script( 'fancy' );
wp_enqueue_style( 'owl-css', get_template_directory_uri() . '/asset/css/owl.carousel.min.css' );
}
}
Der Code dient dazu, die Ladegeschwindigkeit und Ressourcennutzung von WordPress-Webseiten zu optimieren, indem unnötige JavaScript- und CSS-Dateien auf allen Seiten entfernt werden. Gleichzeitig werden notwendige Skripte und Styles nur dort registriert und geladen, wo sie benötigt werden.