首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将属性添加到通过wp_register_style生成的链接标记中?

将属性添加到通过wp_register_style生成的链接标记中?
EN

Stack Overflow用户
提问于 2015-01-26 20:49:34
回答 2查看 3.7K关注 0票数 5

我最初的问题在这里得到了回答:谷歌字体提供:没有‘访问-控制-允许-起源’头是存在于所请求的资源。

有没有办法将数据-noprefix属性添加到我的Google字体链接标签中?

我的functions.php看起来是这样的:

代码语言:javascript
复制
wp_register_script( 'prefixfree', get_template_directory_uri().'/js/prefixfree.min.js', array( 'jquery' ) );
wp_enqueue_script( 'prefixfree' );

wp_register_style( 'google-fonts', 'http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,600,600italic,700,700italic', '', '', 'all' );
wp_enqueue_style( 'google-fonts' );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-27 20:00:57

这对我起了作用:

代码语言:javascript
复制
add_filter( 'style_loader_tag', 'add_noprefix_attribute', 10, 2 );

function add_noprefix_attribute($link, $handle) {
    if( $handle === 'google-fonts' ) {
        $link = str_replace( '/>', 'data-noprefix />', $link );
    }
    return $link;
}
票数 4
EN

Stack Overflow用户

发布于 2018-11-24 11:21:05

style_loader_tag是官方的WordPress API,请参阅文档:标签/

apply_filters( 'style_loader_tag', $html, $handle, $href, $media ) 过滤已排队样式的HTML链接标记。

--首先,将样式表()排队

有关更多信息,请参见文档:风格/风格

代码语言:javascript
复制
function add_styles() {
    // Example loading external stylesheet, could be used in both a theme and/or plugin
    wp_enqueue_style( 'font-awesome-5', 'https://use.fontawesome.com/releases/v5.5.0/css/all.css', array(), null );

    // Example theme
    wp_enqueue_style( 'font-awesome-5', get_theme_file_uri( '/assets/css/fontawesome.min.css' ), array(), null );

    // Example plugin
    wp_enqueue_style( 'font-awesome-5', plugins_url( '/assets/css/fontawesome.min.css', __FILE__ ), array(), null );
};
add_action( 'wp_enqueue_scripts', 'add_styles' );

$handle'font-awesome-5'

我做null,这样就没有版本号了。这样它就会被缓存。

简单字符串替换

一个简单的str_replace就足以实现您想要的结果,参见下面的示例

代码语言:javascript
复制
function add_font_awesome_5_cdn_attributes( $html, $handle ) {
    if ( 'font-awesome-5' === $handle ) {
        return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
    }
    return $html;
}
add_filter( 'style_loader_tag', 'add_font_awesome_5_cdn_attributes', 10, 2 );

添加不同的

下面的示例将不同的属性添加到(多个)不同的样式表中

代码语言:javascript
复制
function add_style_attributes( $html, $handle ) {

    if ( 'font-awesome-5' === $handle ) {
        return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
    }

    if ( 'another-style' === $handle ) {
        return str_replace( "media='all'", "media='all' integrity='blajbsf' example", $html );
    }

    if ( 'bootstrap-css' === $handle ) {
        return str_replace( "media='all'", "media='all' integrity='something-different' crossorigin='anonymous'", $html );
    }

    return $html;
}
add_filter( 'style_loader_tag', 'add_style_attributes', 10, 2 );

向所有样式添加属性()

下面的示例将相同的属性添加到多个样式表中。

代码语言:javascript
复制
function add_attributes_to_all_styles( $html, $handle ) {

        // add style handles to the array below
        $styles = array(
            'font-awesome-5',
            'another-style',
            'bootstrap-css'
        );

        foreach ( $styles as $style ) {
            if ( $style === $handle ) {
                return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
            }
        }

        return $html;
}
add_filter( 'style_loader_tag', 'add_attributes_to_all_styles', 10, 2 );

script_loader_tag

我还想解释一下script_loader_tag,因为这也很方便,但是这个API与脚本一起工作。

script_loader_tag API几乎相同,只是一些小的差异,请参阅文档:标签/

apply_filters( 'script_loader_tag', $tag, $handle, $src ) 过滤队列脚本的HTML脚本标记。

下面是一个延迟单个脚本的示例

代码语言:javascript
复制
function add_defer_jquery( $tag, $handle ) {
    if ( 'jquery' === $handle ) {
        return str_replace( "src", "defer src", $tag );
    }
    return $tag;
}
add_filter( 'style_loader_tag', 'add_defer_jquery', 10, 2 );

下面是一个延迟多个脚本的示例

代码语言:javascript
复制
function add_defer_attribute( $tag, $handle ) {

    // add script handles to the array below
    $scripts_to_defer = array(
        'jquery',
        'jquery-migrate',
        'bootstrap-bundle-js'
    );

    foreach ( $scripts_to_defer as $defer_script ) {
        if ( $defer_script === $handle ) {
            return str_replace( "src", "defer src", $tag );
        }
    }

    return $tag;
}
add_filter( 'script_loader_tag', 'add_defer_attribute', 10, 2 );

所以我已经解释了API的style_loader_tagscript_loader_tag。我给出了两个API的一些例子,我希望这对很多人都有帮助。我对这两个API都有经验。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28158492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档