Support » Themes and Templates » admin options

  • Resolved Demetrius Pop

    (@demetrius-pop)


    Site:
    the site
    Case:
    I have options set up in the admin panel of a child theme allowing the user to change background colors. Those colors are in one of 3 skin.css files that should get called based on user input.

    Problem:
    the skin.css file is not being recognized, as I can see using developer tool. According to the developer tool, the path looks something like this: theme_folder/wp-content/themes/skins//skins.css ( error 404).
    In-between the double slashes (above) is where the variable ($my_path_color_variant) should be yielding the appropriate skin.css file.

    I have this working in a child theme of Thematic. However, even though the options panel gets loaded and is functional (in current child theme of path), the skin.css file isn’t loading. I’m pretty sure it’s a global variable in the options.php file that I’m doing something wrong with; possibly something with the theme name being different (which I’ve changed respectively.

    <?php
    
    // Set some theme specific variables for the options panel
    $childthemename = "my-path Theme";
    $childshortname = "mt08";
    $childoptions = array();
    
    function childtheme_options() {
      global $childthemename, $childshortname, $childoptions;
    
      // Create array to store the Categories to be used in the drop-down select box
      $categories_obj = get_categories('hide_empty=0');
      $categories = array();
      foreach ($categories_obj as $cat) {
        $categories[$cat->cat_ID] = $cat->cat_name;
      }
    
       // the color variants array
      $color_variants = array(
        "blue" => "blue",
        "red" => "red",
        "steel" => "steel"
      );
    
      $childoptions = array (
    
      	 array( "name" => __('Color Variant','path'),
               "desc" => __('Select which color scheme or variant you would like to use.','path'),
               "id" => "my_path_color_variant",
               "std" => 'skin',
               "type" => "radio",
               "options" => $color_variants
        ),
    
        array( "name" => __('Link Color','path'),
               "desc" => __('Change the color of links by entering a HEX color number. (e.g.: 003333)','path'),
               "id" => "my_path_link_color",
               "std" => "999999",
               "type" => "text"
        ),
        array( "name" => __('Show Header Image','path'),
               "desc" => __('Show an image in the header. Replace the header.png file found in the /my-path/images/ folder with your own image, up to 120x100px.','path'),
               "id" => "my_path_show_logo",
               "std" => "false",
               "type" => "checkbox"
        ),
        array( "name" => __('Featured Category','path'),
               "desc" => __('A category of posts to be featured on the front page.','path'),
               "id" => "my_path_feature_cat",
               "std" => $default_cat,
               "type" => "select",
               "options" => $categories
        )
      );
    }
    add_action('init', 'childtheme_options');
    
    //    Make a Theme Options Page
    function childtheme_add_admin() {
      global $childthemename, $childshortname, $childoptions;
    
      if ( $_GET['page'] == basename(__FILE__) ) {
    
        if ( 'save' == $_REQUEST['action'] ) {
          // protect against request forgery
          check_admin_referer('childtheme-save');
          // save the options
          foreach ($childoptions as $value) {
            if( isset( $_REQUEST[ $value['id'] ] ) ) {
              update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
            } else {
              delete_option( $value['id'] );
            }
          }
    
          // return to the options page
          header("Location: themes.php?page=options.php&saved=true");
          die;
    
        } else if ( 'reset' == $_REQUEST['action'] ) {
          // protect against request forgery
          check_admin_referer('childtheme-reset');
          // delete the options
          foreach ($childoptions as $value) {
            delete_option( $value['id'] );
          }
    
          // return to the options page
          header("Location: themes.php?page=options.php&reset=true");
          die;
        }
      }
      add_theme_page($childthemename." Options", "$childthemename Options", 'edit_themes', basename(__FILE__), 'childtheme_admin');
    }
    add_action('admin_menu' , 'childtheme_add_admin');
    
    function childtheme_admin() {
    
      global $childthemename, $childshortname, $childoptions;
    
      // Saved or Updated message
      if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$childthemename.' settings saved.</strong></p></div>';
      if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$childthemename.' settings reset.</strong></p></div>';
    
      // The form
      ?>
    
      <div class="wrap">
      <h2><?php echo $childthemename; ?> Options</h2>
    
      <form method="post">
    
      <?php wp_nonce_field('childtheme-save'); ?>
      <table class="form-table">
    
      <?php foreach ($childoptions as $value) {
    
        // Output the appropriate form element
        switch ( $value['type'] ) {
    
          case 'text':
          ?>
          <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?>:</th>
            <td>
              <input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="text" value="<?php echo stripslashes(get_option( $value['id'], $value['std'] )); ?>" />
              <?php echo $value['desc']; ?>
            </td>
          </tr>
          <?php
          break;
    
          case 'select':
          ?>
          <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
            <td>
              <select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
                <option value="">--</option>
                <?php foreach ($value['options'] as $key=>$option) {
                  if ($key == get_option($value['id'], $value['std']) ) {
                    $selected = "selected=\"selected\"";
                  } else {
                    $selected = "";
                  }
                ?>
                <option value="<?php echo $key ?>" <?php echo $selected ?>><?php echo $option; ?></option>
              <?php } ?>
              </select>
              <?php echo $value['desc']; ?>
            </td>
          </tr>
          <?php
          break;
    
          case 'textarea':
          $ta_options = $value['options'];
          ?>
          <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?>:</th>
            <td>
              <?php echo $value['desc']; ?>
              <textarea name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" cols="<?php echo $ta_options['cols']; ?>" rows="<?php echo $ta_options['rows']; ?>"><?php
                echo stripslashes(get_option($value['id'], $value['std']));
              ?></textarea>
            </td>
          </tr>
          <?php
          break;
    
          case "radio":
          ?>
          <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?>:</th>
            <td>
              <?php foreach ($value['options'] as $key=>$option) {
                if ($key == get_option($value['id'], $value['std']) ) {
                  $checked = "checked=\"checked\"";
                } else {
                  $checked = "";
                }
                ?>
                <input type="radio" name="<?php echo $value['id']; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />
              <?php } ?>
              <?php echo $value['desc']; ?>
            </td>
          </tr>
          <?php
          break;
    
          case "checkbox":
          ?>
          <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
            <td>
              <?php
              if(get_option($value['id'])){
                $checked = "checked=\"checked\"";
              } else {
                $checked = "";
              }
              ?>
              <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
              <?php echo $value['desc']; ?>
            </td>
          </tr>
          <?php
          break;
    
          default:
          break;
        }
      }
      ?>
    
      </table>
    
      <p class="submit">
        <input name="save" type="submit" value="Save changes" class="button-primary" />
        <input type="hidden" name="action" value="save" />
      </p>
    
      </form>
    
      <form method="post">
        <?php wp_nonce_field('childtheme-reset'); ?>
        <p class="submit">
          <input name="reset" type="submit" value="Reset" />
          <input type="hidden" name="action" value="reset" />
        </p>
      </form>
    
      <p><?php _e('For more information about this theme, check out <a href="http://sitepoint.com/books/wordpress1">Build Your Own my-path WordPress Themes</a>. If you have any questions, visit the <a href="http://sitepoint.com/forums/">SitePoint Forums</a>.', 'path'); ?></p>
    
      <?php
    } // end function
    
    ?>

    Style sheet:

    <?php
    
    function my_path_load_custom_styles() {
      // load the custom options
      global $childoptions;
      foreach ($childoptions as $value) {
        $$value['id'] = get_option($value['id'], $value['std']);
      }
    
      wp_enqueue_style(
        'my-path-skin',
        get_bloginfo('stylesheet_directory').'/skins/'.$my_path_color_variant.'/skin.css',
        '', '', 'all'
      );
    
      // output a style sheet with the options
      ?>
    
      <style type="text/css">
        /* <![CDATA[ */
        /* Color Options */
        a, a:link, a:visited,
        #content a,
        #content a:link,
        #content a:visited {color:#<?php echo $my_path_link_color; ?>;}
    
        <?php if ($my_path_show_logo  == 'true') { ?>
    
        #blog-title {
          background:transparent url('<?php echo get_bloginfo('stylesheet_directory') ?>/images/Logo.png') left top no-repeat;
          padding-left: 120px;
          height: 120px;
        }
    
        <?php } ?>
    
        /* ]]> */
      </style>
    
      <?php
    } // end function
    
    add_action('wp_print_styles', 'my_path_load_custom_styles');
    
    add_action('wp_head', 'my_path_load_custom_styles');
    
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘admin options’ is closed to new replies.