wp_enqueue_style and rel other than stylesheet?

 ̄綄美尐妖づ 提交于 2019-12-05 02:47:35

While neither function will let you pass that value in, you do have access to the tag before it is rendered with the style_loader_tag filter. If you do something like this...

add_filter('style_loader_tag', 'my_style_loader_tag_function');

function my_style_loader_tag_function($tag){
  //do stuff here to find and replace the rel attribute

  return $tag;
}

...you should be able to replace the rel attribute with whatever you want. Keep in mind that this filter will pass in the whole tag as html, so you'll have to do a preg_replace() or something similar to replace the value with what you want. Also, this filter will run every time you enqueue a stylesheet, so make sure you test that you've got the right one (with a preg_match() or something) before you alter the rel attribute.

i know this is an old q, but it helped me figure this out. borrowing from brandwaffle's answer here's the full function I used:

function childtheme_scripts() {

wp_enqueue_style('less',get_stylesheet_directory_uri() .'/style.less');
add_filter('style_loader_tag', 'my_style_loader_tag_function');

wp_enqueue_script('less',get_stylesheet_directory_uri() .'/jscripts/less-1.3.0.min.js', false,'1.3.0');

}
add_action('wp_enqueue_scripts','childtheme_scripts', 1);


function my_style_loader_tag_function($tag){
  //do stuff here to find and replace the rel attribute    
  return preg_replace("/='stylesheet' id='less-css'/", "='stylesheet/less' id='less-css'", $tag);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!