I want to add dynamic CSS, so currently I am adding directly to the div, ie:
$color = "#000000";
echo '<div style="background:'.$color.'">
Is it bad to add code like above?
I have read in some forum that it adds server burden and it is not recommended by WordPress coding standards. I should use wp_add_inline_style()
. So I am trying to use that but it does not work. Here's what I'm trying:
functions.php:
function add_custom_css() {
wp_enqueue_script('custom-css', get_template_directory_uri() . '/custom.css');
}
}
add_action( 'wp_enqueue_scripts', 'add_custom_css' );
single.php
$color = "#000111";
$custom_css = "
.mycolor{
background: {$color};
}";
wp_add_inline_style( 'custom-css', $custom_css );
It does not add any code in the custom.css file. What am I doing wrong?