javascript - Get src of scripts and styles loaded in current page -
in wordpress need know src of scripts , style loaded in current page (so in homepage need homepage's scripts , styles).
i've tried code below think these scripts , styles of wordpress site. in fact frontend doesn't work (see image).
my scope styles' hrefs, scripts' srcs , print them (<link href..., <script src...) want.
my custom code in theme's functions.php
/** * creo gli array dei percorsi */ $include_css = array(); $include_js = array(); add_action('wp_print_styles', 'ac'); function ac() { /** * carico le variabili globali contententi stili e scripts */ global $wp_styles, $wp_scripts; global $include_css, $include_js; /** * dichiaro variabili d'utilità */ $base_url = get_site_url(); $external_url = "//"; $css_extension = ".css"; $js_extension = ".js"; /** * per ogni stile */ foreach ( $wp_styles->registered $registered ) { $css = $registered->src; /** * se finisce con .css */ if (endswith($css, $css_extension)) { /** * se non inizia con "//" (risorsa esterna) e se non contiene già l'url di base */ if((!startswith($css, $external_url)) && (!startswith($css, $base_url))) { $include_css[] = $base_url."".$css; } else { $include_css[] = $css; } } } $wp_styles->registered = array(); /** * per ogni script */ foreach ( $wp_scripts->registered $registered ) { $js = $registered->src; /** * se finisce con .js */ if (endswith($js, $js_extension)) { /** * se non inizia con "//" (risorsa esterna) e se non contiene già l'url di base */ if((!startswith($js, $external_url)) && (!startswith($js, $base_url))) { $include_js[] = $base_url."".$js; } else { $include_js[] = $js; } } } $wp_scripts->registered = array(); } function startswith($haystack, $needle) { // search backwards starting haystack length characters end return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false; } function endswith($haystack, $needle) { // search forward starting end minus needle length characters return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false); } add_action( 'wp_footer', 'b' ); add_action( 'wp_head', 'c' ); function b() { global $include_css, $include_js; foreach($include_js $src) { echo " <script type=\"text/javascript\" src=\"$src\"></script> "; } } function c() { global $include_css, $include_js; foreach($include_css $src) { echo " <link rel=\"stylesheet\" type=\"text/css\" href=\"$src\"/> "; } } the frontend after adding custom code: 
Comments
Post a Comment