You could try using the style_loader_tag filter.
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
function my_enqueue_scripts() {
wp_enqueue_style('my-style-handle',
"https://wordpress.stackexchange.com/fonts/custom-font-folder/CustomFontFile.woff2", array(), null);
}
add_filter('style_loader_tag', 'my_style_loader_tag_filter', 10, 2);
function my_style_loader_tag_filter($html, $handle) {
if ($handle === 'my-style-handle') {
return str_replace("rel="stylesheet"",
"rel="preload" as="font" type="font/woff2" crossorigin='anonymous'", $html);
}
return $html;
}
Here we’re enqueuing the stylesheet using the normal wp_enqueue_style
function. We then capture the output using the filter and replace it’s rel
attribute with your updated attributes.
Related Posts:
- Should I use wp_register_style(), wp_enqueue_style, or both?
- How do I dequeue a parent theme’s CSS file?
- Check if a script/style was enqueued/registered
- wp enqueue style on specific page templates
- How to load css in the footer [duplicate]
- How can I get a list of all enqueued scripts and styles?
- Load CSS/Javascript in frontend conditionally if block is used
- Prevent Version URL Parameter (?ver=X.X.X) on Enqueued Styles & Scripts
- How can I de-register ALL styles all at once? And same with Javascript?
- Removing specific style from wp_head
- How to dequeue / deregister any theme styles and scripts
- Notice that the wp_enqueue_style is not being called correctly!
- Hyphens vs. periods in the script slug in wp_register_script?
- Get the list of enqueued/registered scripts for a specific post?
- Trying to load different syles for 404.php page
- What determines the order of enqueued styles?
- $wp_styles->registered not giving ALL the styles?
- How to use wp_enqueue_style() and wp_enqueue_script() only when needed?
- Preload key requests using wp_enqueue
- Remove a script from a template file using wp_dequeue_script
- Remove specific CSS and JS from the head
- How to dequeue / deregister parent theme style
- Enqueued scripts and styles loading in WordPress Dashboard as well
- Add last modified time as version to css and js
- How can I load script and style in specific page in the back-end?
- Best spot for wp_register_script() and wp_register_style()
- Custom Plugin Development: What priority should wp_enqueue_scripts have?
- How to switch css files according to devices and button click?
- How to control the order of the combination of enqueued styles and scripts – site speed issue
- Register script/style: Is it possible to customize the version query string via plugin?
- Asset loading with gulp.js or Grunt
- Adding custom fonts (local) to WordPress?
- How to load script-related styles automatically?
- Optimizing WordPress’s styles and scripts
- How to enqueue a style using wp_enqueue_scripts()?
- Deregister scripts on unnecessary pages using remove_action
- Enqueue Style for a page/pages only
- Why are files enqueued with wp_enqueue_style and wp_enqueue_script 404 Not Found?
- enqueue admin styling and scripts only on plugin page
- using conditionals on enqueue styles
- wp_enqueue_script Doesn’t Work?
- Exclude external uri for css and js version
- How to enqueu php files with custom variable & conditionals?
- Enqueue never runs
- How to enqueue JavaScript file that depends on CSS stylesheet
- How can I properly enqueue tags in WordPress to use with Web Components?
- Load same stylesheet for front end and wp-admin
- Enqueueing Scripts and Styles
- Enqueuing a script before anything else
- How to remove all enqueued assets from the active theme?
- How to add conditionals for IE browsers between certain versions?
- Should we explicitely enqueue jQuery and our theme’s main stylesheet?
- All of my Scripts and Enques are getting file not found
- How to use wp_enqueue_script, style when required
- Enqueueing Scripts/Styles mid-page
- how to load css and js based on post template
- I want to set global directory locations for my CSS and JS locations. How?
- javascript file is not enqueing
- Enqueueing Scripts and Styles to page template that has html
- FullCalendar in WordPress is not working
- Check for Twitter Bootstrap Loaded
- is there a way to get all queued scripts/styles into a template without `get_header()`?
- How do you load js and style resources from CDN with local fallback using wp_enqueue_scripts?
- Adding google fonts to WordPress theme
- Filter enqueued styles and scripts
- WordPress wp_enqueue_style and wp_enqueue_script not working
- Stylesheets and scripts not loading
- Scripts and stylesheets failing to load in Chrome IOS (and only Chrome IOS)
- Enqueue script with version number not working
- How to enqueue a script on a specific URL that contains multiple parts
- i can’t handle any style or Js file on my theme . what is the problem of this file?
- Should I be enqueueing styles/scripts once and then dequeueing them?
- List all non-admin enqueued files
- Most optimal way of starting fresh, without using other theme or dropping old files?
- wp_enqueue doesn’t load dependencies
- wp_enqueue_scripts hangs
- Why do WordPress adds the id=”handle-{js|css}” attribute to scripts and stylesheet?
- When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()?
- How to load JQuery easing script in wordpress?
- Benefits to enqueuing site scripts/stylesheets?
- Dynamically Load Styles and Scripts from Theme Functions.php
- How do I load custom scripts and styles for a page?
- Using multiple versions of jQuery while still calling it like WP likes
- Is my approach to enqueue styles inefficient?
- debugging js scripts
- Unable to dequeue a plugin script. I think I’ve tried everything
- Enqueue script/style if certain widget has certain value
- Action hooked on wp_enqueue_scripts running twice
- wp_enqueue_scripts wont load my scripts
- JavaScript enque stopped working
- How would one set or override a Fallback font for characters in second language?
- load jquery in footer not working
- Stop a plugins js and css from loading on all pages and show up only where it is called
- plugin css is not being applied to the page
- Adding styles/scripts to specific page
- How do I dequeue a Stylesheet, stored in an ‘Assets’ folder?
- How to load default wordpress scripts
- Generating embed code for users to share
- Registering / Enqueing Scripts Not Working
- plugin not loading my css and js