• Hello,

    I pasted following code in functions.php file:

    function my_styles_method() {
        	wp_enqueue_style(
        		'custom-style',
        		get_template_directory_uri() . '/css/custom_script.css'
        	);
                $color = #FF0000; //E.g. #FF0000
                $custom_css = "
                        .mycolor{
                                background: {$color};
                        }";
                wp_add_inline_style( 'custom-style', $custom_css );
        }
        add_action( 'wp_enqueue_scripts', 'my_styles_method' );

    custom_script.css file is displayed in source of website but file is just empty, chmod of file is 644

    https://codex.ww.wp.xz.cn/Function_Reference/wp_add_inline_style

Viewing 2 replies - 1 through 2 (of 2 total)
  • anonymized-13749270

    (@anonymized-13749270)

    Are you trying to write this file custom_script.css with your CSS codes? use file creating and writing in PHP

    I would just queue the inline CSS to head section like this:

    add_action('wp_head', 'enqueue_my_css');
    function enqueue_my_css() {
    	?>
    		<style type="text/css" media="all">
    
    		.mycolor {
    			background: #FF0000;
    		}
    
    		</style>
    	<?php
    }
    Thread Starter m1000

    (@m1000)

    OK, so wp_add_inline_style doesn’t add style do css file but it adds style after custom_script.css inside:

    <style type="text/css" media="all">
    ...
    </style>
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_add_inline_style doesn't work’ is closed to new replies.