获取当前页面中加载的脚本和样式的src

时间:2015-07-13 14:55:03

标签: javascript php css wordpress

在Wordpress中我需要知道当前页面中加载的所有脚本和样式的src(因此在主页中我只需要主页的脚本和样式)。

我已经尝试了下面的代码,但我认为这些是我的Wordpress网站的所有脚本和样式。事实上,前端没有正常工作(见图)。

我的目标是获得所有款式&#39; hrefs,所有脚本&#39; srcs然后打印它们(<link href ...,<script src ...)我想要的地方。

我主题的自定义代码function.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 as $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 as $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 from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
function endsWith($haystack, $needle) 
{
    // search forward starting from 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 as $src)
    {
        echo 
        "
            <script type=\"text/javascript\" src=\"$src\"></script>
        ";
    }
}
function c()
{
    global $include_css, $include_js;
    foreach($include_css as $src)
    {
        echo 
        "
            <link  rel=\"stylesheet\" type=\"text/css\" href=\"$src\"/>
        ";
    }
}

添加自定义代码后的前端: enter image description here

0 个答案:

没有答案