Note that there are some explanatory texts on larger screens.

plurals
  1. POSwitch statement not changing CSS in a WordPress theme options page
    primarykey
    data
    text
    <p>I'm creating a theme options page for my new theme and I set up some radio buttons to change the style when a user clicks each button. After saving the changes, it is supposed to add a stylesheet to the header of the theme. It is not working correctly. I assume that my switch statement might be written incorrectly. I'm using WordPress's Settings API. Below is most of the code in question.</p> <pre><code>function mxs_admin_init() { register_setting( 'mixinstyles_theme_options', 'mixinstyles_theme_options', 'mixinstyles_options_validate' ); add_settings_section( 'mixinstyles_main', 'Mixin&amp;apos; Styles Settings', 'theming_section_text', 'mixinstyles' ); add_settings_field( 'custom_style_buttons', '&lt;strong&gt;Color Schemes&lt;/strong&gt;', 'custom_style_buttons', 'mixinstyles', 'mixinstyles_main' ); } ... function mxs_theme_options_page() { ?&gt; &lt;div class="wrap" style="margin-bottom: 20px;"&gt; &lt;div id="icon-themes" class="icon32"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2&gt;Mixin' Styles Theme Options&lt;/h2&gt; &lt;?php if($_REQUEST['settings-updated'] == 'true') { echo '&lt;div id="message" class="updated fade"&gt;&lt;p&gt;Mixin&amp;apos; Styles options saved.&lt;/p&gt;&lt;/div&gt;'; } ?&gt; &lt;form action="options.php" method="post" name="options_form"&gt; &lt;?php settings_fields('mixinstyles_theme_options'); ?&gt; &lt;?php do_settings_sections('mixinstyles'); ?&gt; &lt;div style="text-align: center; padding: 20px;"&gt;&lt;input name="Submit" class="button-primary" type="submit" value="&lt;?php esc_attr_e('Save Changes'); ?&gt;" /&gt;&lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;?php } ... function custom_style_buttons() { $options = get_option('mixinstyles_theme_options'); echo "&lt;div class='radiobutton-wrap'&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' id='default_style' value='default_style' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/default_screenshot.png' alt='Default style' /&gt;&lt;br /&gt;&lt;label for='default_style'&gt;Default Style&lt;/label&gt; &lt;/div&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' style='padding-left: 10px; padding-right: 10px;' id='blue_orange' value='blue_orange' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/blueorange_screenshot.png' alt='Blue/Orange style' /&gt;&lt;br /&gt;&lt;label for='blue_orange'&gt;Blue/Orange&lt;/label&gt; &lt;/div&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' id='violet_yellow' value='violet_yellow' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/violetyellow_screenshot.png' alt='Violet/Yellow style' /&gt;&lt;br /&gt;&lt;label for='violet_yellow'&gt;Violet/Yellow&lt;/label&gt; &lt;/div&gt; \n"; echo "&lt;/div&gt; \n"; echo "&lt;div class='radiobutton-wrap'&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' id='magenta_green' value='magenta_green' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/magentagreen_screenshot.png' alt='Magenta/Green style' /&gt;&lt;br /&gt;&lt;label for='magenta_green'&gt;Magenta/Green&lt;/label&gt;&lt;/div&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' id='orange_blue' value='orange_blue' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/orangeblue_screenshot.png' alt='Orange/Blue style' /&gt;&lt;br /&gt;&lt;label for='orange_blue'&gt;Orange/Blue&lt;/label&gt;&lt;/div&gt; \n"; echo "&lt;div class='radiobutton-padding'&gt; \n &lt;input type='radio' id='yellow_violet' value='yellow_violet' name='mixinstyles_theme_options[custom_style_buttons]' /&gt;&lt;img src='" . get_bloginfo('template_directory') . "/images/yellowviolet_screenshot.png' alt='Yellow/Violet style' /&gt;&lt;br /&gt;&lt;label for='yellow_violet'&gt;Yellow/Violet&lt;/label&gt;&lt;/div&gt; \n"; echo "&lt;/div&gt; \n"; } ... function mxs_style_switcher() { //global $mixinstyles_theme_options; $options = get_option('mixinstyles_theme_options'); //$blue_orange = $options['blue_orange']; switch ( $options['custom_style_buttons'] ) { //opens switch statement case "blue_orange": echo '"\n" . &lt;link rel="stylesheet" href="'; bloginfo('template_directory'); echo '/custom-styles/blue-orange.css" type="text/css" /&gt; . "\n";'; break; case "violet_yellow": echo '"\n" . &lt;link rel="stylesheet" href="'; bloginfo('template_directory'); echo '/custom-styles/violet-yellow.css" type="text/css" /&gt; . "\n";'; break; case "magenta_green": echo '"\n" . &lt;link rel="stylesheet" href="'; bloginfo('template_directory'); echo '/custom-styles/magenta-green.css" type="text/css" /&gt; . "\n";'; break; case "orange_blue": echo '"\n" . &lt;link rel="stylesheet" href="'; bloginfo('template_directory'); echo '/custom-styles/orange-blue.css" type="text/css" /&gt; . "\n";'; break; case "yellow_violet": echo '"\n" . &lt;link rel="stylesheet" href="'; bloginfo('template_directory'); echo '/custom-styles/yellow-violet.css" type="text/css" /&gt; . "\n";'; break; default: echo ''; } //closes switch statement } </code></pre> <p>In the header theme template, I'm calling the style switcher like this:</p> <pre><code>&lt;?php $mxs_settings = get_option('mixinstyles_theme_options'); echo $mxs_settings['mxs_style_switcher']; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload