Use CSS value from theme style options page? (4 posts)

  1. Rakesh Raja
    Posted 3 years ago #


    I am making a theme options page for my theme.

    I would like to have style options like change color for fonts.. font size etc you know CSS options!

    I have setup the theme options but how can I get value in theme CSS file? or how can I create a dynamic CSS file?

    I checked few themes and they are putting this CSS code in header.. Is it good way?

  2. bcworkz
    Posted 3 years ago #

    I hope you don't mean the code is altering the header file!

    I would store the values in the options table, then output the corresponding CSS to the header at runtime via the wp_head() call. This is done by hooking the 'wp_head' action and echoing out your CSS style block. This works well because CSS on the page overrides any included CSS files with the same directives.

  3. Rakesh Raja
    Posted 3 years ago #

    Can you provide a sample code please? How to use wp_head ?

  4. bcworkz
    Posted 3 years ago #

    Assuming your options page has previously added a valid color value to the options table under the key 'rak_body_color', do something like:

    function rak_style_head() {
      $color = get_option('rak_body_color');
      echo "<style>body {color: $color;}</style>";
    add_action('wp_head', 'rak_style_head');

Topic Closed

This topic has been closed to new replies.

About this Topic