I am trying to inject a inline style css to my body element via the functions.php file. It needs to be inline, because I am using a ACF to let the user change the image.
This should be the result:
<body style="background-image: url('<?php the_field('background'); ?>');">
I read about the wp add inline style, but i couldn't figure it out.
Update: Here is the function I tried:
function wpdocs_styles_method() {
wp_enqueue_style('custom-style', get_stylesheet_directory_uri() . '/body.css'
);
$img = get_theme_mod( "<?php the_field('background') ?>" );
$custom_css = "body {background-image: {$img}";
wp_add_inline_style( 'custom-style', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_styles_method' );
I did load a body.css to tried to add the inline css. But it didn't work - maybe this isn't the right approach at all?
<?php
and?>
tags, since you're already parsing PHP. Also, you should be usingget_field()
since you don't want to beecho
ing the returned value.