* wp_enqueue_script_special('jquery','',array(),false,false,'async') Wp_enqueue_script($handle, $srcString, $depArray, $version, $inFooter) For example, if you wanted to change all non-https scripts to https, just in case a plugin tries to add one, you could do something like this: ` or ``) The way both of the do_item() filters work is that it passes a bunch of arguments to a function you hook into the filter with add_filter(), and it expects that your function returns a string that represents the new or tag that it should use. Since WordPress is open source, we can find out!Īfter a little digging, we can see that the function that actually turns queued scripts and styles into echoed out and tags is do_item($handle), which is a public method on the WP_Scripts and WP_Styles classes. If you start to search for how to modify the HTML that is generated by “wp_enqueue_style” and “wp_enqueue_script”, you will quickly come across posts recommending that you hook into filters, “style_loader_tag” and “script_loader_tag” respectively. Where are the filter calls actually generated I started looking into this since I wanted to be able to add defer or async loading to scripts and styles, which requires modifying attributes of the outputted tags. To do anything like this requires using WordPress Filter, which is not always straightforward. For example, WP devs are familiar with ‘ wp_enqueue_script()‘ in PHP, which will generate a tag output in HTML, but there are no arguments on that method to add attributes or even to simply grab the raw text output of it. One of the downsides to WordPress is that they don’t always have the best exposed methods / APIs for modifying how HTML generated by their own system (rather than your content like posts or pages) gets echoed out.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |