如何修改Wordpress样式表的输出或属性排队?

时间:2016-06-13 02:22:50

标签: php wordpress filter

默认情况下,WP将样式表排列为:

<link rel="stylesheet" id="contact-form-7-css" href="http://www.wecodeart.com/wp-content/plugins/contact-form-7/includes/css/styles.css" type="text/css" media="all">

我想要一个过滤器或函数来为此链接添加另一个属性,例如property =“stylesheet”......我可以这样做吗?

2 个答案:

答案 0 :(得分:1)

是的,str_replace是可能的

这是代码

add_filter('style_loader_tag', 'style_loader_tag_function', 10, 2);

function style_loader_tag_function($tag, $handle) {



 echo $tag;

 $tag = str_replace( 'rel="stylesheet"', 'rel="stylesheet/less"', $tag );

if($handle=="contact-form-7")
{

$tag = str_replace( "rel='stylesheet'", "rel='stylesheet' property='stylesheet'", $tag );
}
    return $tag;
}

答案 1 :(得分:0)

试试这个:

在当前激活的主题的function.php中使用clean_url过滤器。

function add_my_custom_attributes( $url )
{
    $enqueue_attr = array (
        'http://www.wecodeart.com/wp-content/plugins/contact-form-7/includes/css/styles.css' //can also use site_url function      
    );   
    if ( in_array( $url, $enqueue_attr ) )
    { // this will be optimized
        return "$url' data-YOUR_NEW_ARRT='VALUE";
    }    
    return $url;
}
add_filter( 'clean_url', 'add_my_custom_attributes', 99, 1 );