I want to add inline styles thanks to the wp_add_inline_style wordpress function.
The inline styles will be added in a loop. In my case, I output an isotope grid in my loop and this grid can have differents css customization for each grid.
Actually I have this php script:
function grid_register_styles(){
$themeversion = wp_get_theme()->display('Version');
wp_register_style( 'grid-style', get_bloginfo('stylesheet_url'), array(), floatval($themeversion) , false );
}
add_action( 'init', 'grid_register_styles' );
function grid_inline_styles($guttercss) {
wp_enqueue_style( 'grid-style' );
wp_add_inline_style( 'grid-style', $guttercss);
}
/* this is the function used for the loop */
function grid_init() {
/* There is some stuff before */
/* I calculate a variable in order to apply a style base on some options */
$guttercss = '.gutter-'. $gutter .' .to-item-wrapper {';
$guttercss .= 'margin: '. $gutter/2 .'px !important;}';
/* Then I call my function to output inline css style */
grid_inline_styles($guttercss);
/* There is some stuff after */
}
This code works in the loop and output different css style for each generated isotope grid.
However, the generated css is in the footer instead of the head....
I success to put it in the head with an add_action() but it can't only works outside the loop and only one time.
I think that I don't correctly understand the way to use wp_add_inline_style().