0

i got an options page that needs to affects a certain (for now) css value..

i have about using php variables in css and creating custom global variables in wordpress but havent something easy enough to understand so i have done this and places it in my header file

<style type="text/css"> .current-menu-item { background: <?php echo get_option('sg_currentbuttoncolor'); ?> !important; } </style>

does that look like a good solution to you? what is the preffered way of creating changes in my css using values from my options page?

1 Answer 1

2

You are doing it correctly. I would suggest two enhancements:

  1. Do not put this code directly in header.php. Create a custom function to print the css and hook this into the wp_head action.
  2. Always sanitize/escape input before you print it to the screen. Look into ctype_xdigit() for creating a custom function to sanitize you color value.
2
  • Wouldn't it be more appropriate to hook this into wp_print_styles?
    – tollmanz
    Aug 30, 2011 at 1:07
  • Maybe ... but then you need to check if you're in the admin which is unnecessary IMHO. I like to follow the rule "Hook in as late as possible, but no later". wp_head fits the bill rather well and it should only fire in the front-end.
    – mfields
    Aug 30, 2011 at 1:18

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.