I want a random CSS style file to load on my site with each page view so I can play with colors backgrounds.

So I put all my styles in a folder, I named them 1.css, 2.css, etc. and I have added:

<link rel='stylesheet' id='name-random-color-css' href='http://www.url.com/wp-content/themes/themename/inc/rcolor/<?php echo mt_rand(1, 9) ?>.css' type='text/css' media='all' />

in the header.

It works but I know this is not the proper way to include styles in WordPress. I should utilize the wp_enqueue_style function in the functions.php file, but I don't know how to combine the two to make it work.

1 Answer 1


This should work!

function enqueue_random_style() {
        wp_enqueue_style( 'style-name', get_stylesheet_directory_uri() . '/inc/rcolor/' . mt_rand(1, 9));
add_action( 'wp_enqueue_scripts', 'enqueue_random_style' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.