Read below if you want to start from where I left off in core. But the basic question is: I need to add a “title” attribute to my stylesheets and wp_enqueue_style() doesn’t allow for that parameter, as far as I can tell. Other than a hard embed, are there any ways WordPress allows us to add the title to a stylesheet?
In digging around core I notice that there’s a $title
variable that can be set using $style->extra['title']
.
$title = isset($this->registered[$handle]->extra['title']) ? "title='" . esc_attr( $this->registered[$handle]->extra['title'] ) . "'" : '';
(class.wp-styles.php)
And $title also figures in the filter that is applied when you enqueue a stylesheet. So how can you set that ‘extra’ array within the style object?
Okay, here’s where I’m at with a solution.
Don’t like using the global. Is there something better?
It seems to me that you could also use style_loader_tag, see my other answer for a more detailed take on both style_loader_tag and script_loader_tag API:
How to add crossorigin and integrity to wp_register_style? (Font Awesome 5)
style_loader_tag is an official WordPress API, see the documentation: https://developer.wordpress.org/reference/hooks/style_loader_tag/
First, enqueue your stylesheets
Second, use style_loader_tag
Than use style_loader_tag to add your title to the specific stylesheet.
Looking at the file you mentioned in your post class.wp-styles.php, you have the following line,
$tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />n", $handle );
. You can therefore hook into the “style_loader_tag” filter and add in the title tag. I like your answer as well, but am unsure as to which one would be better, as I am not sure if there are any issues with using the global or not.I used the way with
style_loader_tag
. Thereby I missused the handle to transport my additional title-tag. This looks like the following:So I look for
-X
and all after that string is my title attribute.