The problem is that the $opt_val
global variable is being set only when the my_plugin_options
function is called. And that function is being called only in case user is viewing the My Plugin Options screen.
You’ll have to call get_option( 'mt_favorite_color' );
inside the hook_css
function. (Please note that the hook_css
function is not being prefixed. All custom functions, global variables, options etc really shold be prefixed).
Further, the code you shared is not sanitisating values on user input, nor escaping theme on the output. Make sure to read Validating Sanitizing and Escaping User Data part of the codex. and use sanitize_text_field
before saving the value to the database, and esc_attr
when outputting the value in order to make the plugin more secure.