Created
June 28, 2014 07:27
-
-
Save palpalani/a7cbc6991c312031d11e to your computer and use it in GitHub Desktop.
WordPress - Enqueueing Inline CSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ever needed to include a variable, inline style for your plugin or theme? Of course you did! Enqueueing external CSS files with WordPress' wp_enqueue_style() function (and its companions) is pretty slick but it lacks the funcionality to include inline CSS styles. | |
Well, at least that's what I thought before coming across the wp_add_inline_style() function. | |
Usage | |
<?php | |
$custom_style_file = get_template_directory_uri() . '/css/custom_style.css'; | |
function custom_styles() { | |
wp_enqueue_style( 'custom-style', $custom_style_file ); | |
$headline_font_weight = get_theme_mod( 'headline-font-weight' ); | |
$custom_style = '.headline { font-weight: ' . $headline_font_weight . '; }'; | |
wp_add_inline_style( 'custom-inline-style', $custom_style ); | |
} | |
add_action( 'wp_enqueue_scripts', 'custom_styles' ); | |
?> | |
Honestly, I didn't know this function for a long time. It could have saved me a lot of time if I knew that before. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment