Last active
September 21, 2024 21:06
-
-
Save sfnprg/07b8e409827c80f8d7857696b52d8e4a to your computer and use it in GitHub Desktop.
[Wordpress] display scripts loaded in `wp_head` and `wp_footer`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A simple Wordpress function to add to your `functions.php` file in your theme to display | |
* stack of loaded scripts in `wp_head` and `wp_footer`. | |
* Can be used to control loading, get list/name to add defer or async | |
* Originally inspired by https://www.myhostingfacts.com/optimize-css-delivery-inline-and-defer-javascript-wordpress/ | |
* @link https://www.yesweweb.com | |
* | |
* Author: Yes We Web | |
* Author URI: https://www.yesweweb.com | |
*/ | |
if ( ! function_exists( 'yww_display_wp_scripts_in_use' ) ) : | |
if ( current_user_can( 'administrator' ) ) : | |
add_action( 'wp_head', 'yww_display_wp_scripts_in_use', 9999 ); | |
add_action( 'wp_footer', 'yww_display_wp_scripts_in_use', 9999 ); | |
function display_wp_scripts_in_use(){ | |
global $wp_scripts; | |
if ( did_action( 'wp_footer' ) === 1 ) { | |
$section = "wp_footer"; | |
if ( isset( $_SESSION["loaded"] ) ) | |
$loaded_scripts = array_diff( $wp_scripts->done, $_SESSION["loaded"] ); | |
else | |
$loaded_scripts = $wp_scripts->done; | |
} elseif ( did_action( 'wp_head' ) === 1 ) { | |
$section = "wp_head"; | |
$loaded_scripts = $wp_scripts->done; | |
$_SESSION["loaded"] = $wp_scripts->done; | |
} | |
echo "<div style='background-color:#ccc; border:2px dashed #333; padding:10px 30px; margin-top:32px;'>"; | |
echo "<h6 style='margin:0;'>". $section ."</h6>"; | |
echo "<pre>"; print_r($loaded_scripts); echo "</pre>"; | |
echo "</div>"; | |
} | |
endif; | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment