0

The code below is my theme options page (submenu - under "Appearance"). All options work fine except "select" and "radio"... They don't appear in the page as the other options do. I have been searching around the internet and trying different solutions for 3 days but there were no ways to make them appear. After that I would like to seperate the sections in different tabs which is also something I can't do. I have read the related documentation and some tutorials I found but nothing did the trick yet... Any help? Thanks!

    <?php
$themename = "WIS";

$shortname = "wis";

$version = "1.1";

$option_group = $shortname.'_theme_option_group';
$option_name = $shortname.'_theme_options';


add_action('admin_init', 'wis_add_init');

function wis_add_init() {
    $file_dir = get_template_directory_uri();

    wp_enqueue_style("wisCss", $file_dir."/functions/theme-options.css", false, "1.0", "all");

    wp_enqueue_script("wisScript", $file_dir."/functions/theme-options.js", false, "1.0");

}

// Create custom settings menu
add_action('admin_menu', 'wis_create_menu');

function wis_create_menu() {
    global $themename;
    //create new top-level menu
    add_theme_page( __( $themename.' Theme Options' ), __( 'Theme Options' ), 'edit_theme_options', 'wisgentheoptpag', 'wis_settings_page' );
}

// Register settings
add_action( 'admin_init', 'register_settings' );

function register_settings() {
   global $themename, $shortname, $version, $wis_options, $option_group, $option_name;
    //register our settings
    register_setting( $option_group, $option_name);
}

// Create theme options

global $wis_options;

$wis_options = array (

array("name" => __('RSS Feeds/Facebook/Twitter','wis'),

        "type" => "section"),

array("name" => __('Set up social links.','wis'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __('Custom Feed URL','wis'),

        "desc" => __('You can use your own feed URL (<strong>with http://</strong>). Paste your Feedburner URL here to let readers see it in your website.','wis'),

        "id" => "feedurl",

        "type" => "text",

        "std" => get_bloginfo('rss2_url')),

array("name" => __('Delete Extra Feeds','wis'),

        "desc" => __('WordPress adds feeds for categories, tags, etc., by default. Check this box to remove them and reduce the clutter.','wis'),

        "id" => "cleanfeedurls",

        "type" => "checkbox",

        "std" => ""),

array("name" => __('Twitter ID','wis'),

        "desc" => __('Your Twitter user name, please. It will be shown in the navigation bar. Leaving it blank will keep the Twitter icon supressed.','wis'),

        "id" => "twitterid",

        "type" => "text",

        "std" => ""),

array("name" => __('Facebook Page','wis'),

        "desc" => __('Link to your Facebook page, <strong>with http://</strong>. It will be shown in the navigation bar. Leaving it blank will keep the Facebook icon supressed.','wis'),

        "id" => "facebookid",

        "type" => "text",

        "std" => ""),

array("type" => "close"),

//FOOTER

array("name" => __('Footer','wis'),

        "type" => "section"),

array("name" => __('Customize footer of your website.','wis'),

        "type" => "section-desc"),

array("type" =>"open"),

array("name" => __('Footer Text','wis'),

        "desc" => __('Enter your footer text or HTML here.','wis'),

        "id" => "footer_text",

        "std" => "",

        "type" => "textarea"),

array("name" => __('Hide Footer Navigation Links','wis'),

        "desc" => __('Select to hide the navigation bar in the footer. If you want to customize the footer navigation, go to Menus under the Appearance tab in the dashboard.','wis'),

        "id" => "hide_footer_nav",

        "std" => "",

        "type" => "checkbox"),

array("name" => __('Hide Footer Credits','wis'),

        "desc" => __('Select to hide the credit line in the footer. Thanks for sending us a <strong>generous contribution</strong>.','wis'),

        "id" => "hide_footer_credit",

array("name" => __('Theme Styles','wis'),

        "type" => "section"),

array("name" => __('Choose a color scheme and add custom CSS styles.','wis'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __('Colour Scheme','wis'),

        "desc" => __('Select a colour scheme for the theme. Future versions will have multiple styles.','wis'),

        "id" => "alt_stylesheet",

        "type" => "select",

        "options" => $alt_stylesheets,

        "std" => "default.css"),

array( "name" => __('Custom Styles','wis'),

    "desc" => __('Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}','wis'),

    "id" => "custom_css",

    "type" => "textarea",

    "std" => ""),

array("type" => "close"),

        "std" => "",

        "type" => "checkbox"),

array( "type" => "close"),

//ADVERTISEMENTS --- POST ADS

array("name" => __('Advertisements','wis'),

    "type" => "section"),

array("name" => __('Show ads on your blog.','wis'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __('Header Display Ad','wis'),

        "desc" => __('You can show a display ad in header. Paste the code here for 600px by 60px ad.','wis'),

        "id" => "topbanner",

        "std" => "",

        "type" => "textarea"),

array("name" => __('Ad Above Posts','wis'),

        "desc" => __('Enter your Adsense code or other ad network code here. This ad will be displayed at the beginning of posts, below title on Post Pages and Pages with ad-supporting template. It is very basic and effective option for putting ads on your blog. If you want more functionality, get a specialized Ad plugin.','wis'),

        "id" => "posttop_adcode",

        "std" => "",

        "type" => "textarea"),

array("name" => __('Ad Below Posts','wis'),

        "desc" => __('Enter your Adsense code (or other ad network code) here. This ad will be displayed at the end of post content on Post Pages and Pages with ad-supporting template. Please make sure that you do not activate more ads than what is allowed by your ad network. Adsense allows up to 3 on one page.','wis'),

        "id" => "postend_adcode",

        "std" => "",

        "type" => "textarea"),

array("type" => "close"),

//Analytics Code

array("name" => __('Tracking & Other Codes','wis'),

array("name" => __('Theme Styles','wis'),

        "type" => "section"),

array("name" => __('Choose a color scheme and add custom CSS styles.','wis'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __('Colour Scheme','wis'),

        "desc" => __('Select a colour scheme for the theme. Future versions will have multiple styles.','wis'),

        "id" => "alt_stylesheet",

        "type" => "select",

        "options" => $alt_stylesheets,

        "std" => "default.css"),

array( "name" => __('Custom Styles','wis'),

    "desc" => __('Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}','wis'),

    "id" => "custom_css",

    "type" => "textarea",

    "std" => ""),

array("type" => "close"),


        "type" => "section"),

array("name" => __('Insert Web tracking & analytics and other codes here.','wis'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __('Analytics & Tracking Code','wis'),

        "desc" => __('You can paste your Google Analytics or other codes in this box. The codes will be automatically added to the footer.','wis'),

        "id" => "analytics_code",

        "type" => "textarea",

        "std" => ""),

array("type" => "close")

);

function wis_settings_page() {
   global $themename, $shortname, $version, $wis_options, $option_group, $option_name;
?>

<div class="wrap">
<div class="options_wrap">
<?php screen_icon(); ?><h2><?php echo $themename; ?> <?php _e('Theme Options','wis'); ?></h2>
<p class="top-notice"><?php _e('Customize your WordPress blog with these settings. ','wis'); ?></p>
<?php if ( isset ( $_POST['reset'] ) ): ?>
<?php // Delete Settings
global $wpdb, $themename, $shortname, $version, $wis_options, $option_group, $option_name;
delete_option('wis_theme_options');
wp_cache_flush(); ?>
<div class="updated fade"><p><strong><?php _e( $themename. ' options reset.' ); ?></strong></p></div>

<?php elseif ( isset ( $_REQUEST['updated'] ) ): ?>
<div class="updated fade"><p><strong><?php _e( $themename. ' options saved.' ); ?></strong></p></div>
<?php endif; ?>

<form method="post" action="options.php">

<?php settings_fields( $option_group ); ?>

<?php $options = get_option( $option_name ); ?>


<?php foreach ($wis_options as $value) {
if ( isset($value['id']) ) { $valueid = $value['id'];}

switch ( $value['type'] ) {

case "section":

?>

    <div class="section_wrap">

    <h3 class="section_title"><?php echo $value['name']; ?>

<?php break;

case "section-desc":

?>

    <span><?php echo $value['name']; ?></span></h3>

    <div class="section_body">

<?php

break;

case 'text':

?>

    <div class="options_input options_text">

        <div class="options_desc"><?php echo $value['desc']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

        <input name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value['std']); } ?>" />

    </div>

<?php

break;

case 'textarea':

?>

    <div class="options_input options_textarea">

        <div class="options_desc"><?php echo $value['desc']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

        <textarea name="<?php echo $option_name.'['.$valueid.']'; ?>" type="<?php echo $option_name.'['.$valueid.']'; ?>" cols="" rows=""><?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value['std']); } ?></textarea>

    </div>

<?php

break;

case 'select':

?>

    <div class="options_input options_select">

        <div class="options_desc"><?php echo $value['desc']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

        <select name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>">

        <?php foreach ($value['options'] as $option) { ?>

                <option <?php if ($options[$valueid] == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>

        </select>

    </div>

<?php

break;

case "radio":

?>

    <div class="options_input options_select">

        <div class="options_desc"><?php echo $value['desc']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

          <?php foreach ($value['options'] as $key=>$option) {
            if ( isset ( $options[$valueid] ) ) {
                if ($key == $options[$valueid] ) {
                    $checked = "checked=\"checked\"";
                    } else {
                        $checked = "";
                    }
            }else{
                if($key == $value['std']){
                    $checked = "checked=\"checked\"";
                }else{
                    $checked = "";
                }
            }?>

            <input type="radio" id="<?php echo $option_name.'['.$valueid.']'; ?>" name="<?php echo $option_name.'['.$valueid.']'; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />

            <?php } ?>

    </div>

<?php

break;

case "checkbox":

?>

    <div class="options_input options_checkbox">

        <div class="options_desc"><?php echo $value['desc']; ?></div>

        <?php if( isset( $options[$valueid] ) ){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>

        <input type="checkbox" name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>" value="true" <?php echo $checked; ?> />

        <label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label>

     </div>

<?php

break;

case "close":

?>

</div><!--#section_body-->

</div><!--#section_wrap-->

<?php
break;

}

}

?>

<span class="submit">
<input class="button button-primary" type="submit" name="save" value="<?php _e('Save All Changes', 'wis') ?>" />
</span>
</form>

<form method="post" action="">

<span class="button-right" class="submit">

<input class="button button-secondary" type="submit" name="reset" value="<?php _e('Reset', 'wis') ?>" />
<input type="hidden" name="action" value="reset" />
<span id="caution"><?php _e('<span id="emph">Caution: All entries will be deleted from database.</span>','wis') ?></span>

</span>

</form>
</div><!--#options-wrap-->
</div>

<?php } ?>
3
  • have you defined $alt_stylesheets on page? I can't see any value assigned to that variable..are you getting value of text field in template? Commented Jun 6, 2012 at 16:14
  • Thanks for your reply pixelngrain. It says I have to put this code "<?php // Custom CSS block in Theme Options Page if ( isset ($options['custom_css']) && ($options['custom_css']!="") ) { $output = '<style type="text/css">'."n"; $output .= $options['custom_css'] . "n"; $output .= '</style>'."n"; echo $output; } if ( isset ($options['feedurl']) && ($options['feedurl']!="") ) { echo '<link rel="alternate" type="application/rss+xml" href="'.$options['feedurl'].'" title="'. get_bloginfo('name') .' RSS Feed"/>'."n"; }?>" I suppose in header, right? But where? As it is?
    – Arg Geo
    Commented Jun 6, 2012 at 16:28
  • for the selection dropdown you may need to use case see my answer I am posting code for that Commented Jun 6, 2012 at 17:38

1 Answer 1

1

As per my experience isset will not work for text, textarea it will work for checkbox, radio etc (may be someone can guide you in depth for this)

To set option with dropdown selection I would prefer to use switch and case as below

<?php
    switch (get_option('your_option_id')) {

                case "Default": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/default.css" media="screen" />
                <?php break;

                case "Red": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/red.css" media="screen" />
                <?php break;

                case "Blue": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/blue.css" media="screen" />
                <?php break;

                case "Green": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/green.css" media="screen" />
                <?php break;

                case "Yellow": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/yellow.css" media="screen" />
                <?php break;

                case "Black": ?>
                <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/black.css" media="screen" />
                <?php break;

            }
?>

This you can directly place into the header where we call stylesheet or you can create function for that too. I usually create function so theme file doesn't become too large.

to check isset you can use != for text and that will work without any issue so can use something below.

<?php
    if(get_option('myfeed_url') != '') {

        //do this (place your code here)

    }
?>

As I say in above line. You can directly place code to the appropriate place (in your case header.php tab may be) or can create function in function.php and than you have to call the function at appropriate place.

Hope this is what you are looking for

---[ Use This Tested Code ]-------------------------------------------------

<?php

$themename = "WIS";
$shortname = "wis";

$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category"); 

// your stylesheet selection options
//$alt_stylesheets = array("default.css" => "Default", "blue.css" => "Blue", "white.css" => "White", "visual.css" => "Visual");

// or you can directly call value with below line

$alt_stylesheets = array("default.css", "blue.css", "white.css", "visual.css");

$options = array (

array( "name" => $themename." Options",
    "type" => "title"),


array( "name" => "Your section title here",
    "type" => "section"),
array( "type" => "open"),

array("name" => __('Colour Scheme','wis'),
        "desc" => __('Select a colour scheme for the theme. Future versions will have multiple styles.','wis'),
        "id" => "alt_stylesheet",
        "type" => "select",
        "options" => $alt_stylesheets,
        "std" => "white.css"),

array("name" => __('Colour Scheme','wis'),
        "desc" => __('Select a colour scheme for the theme. Future versions will have multiple styles.','wis'),
        "id" => "alt_no",
        "type" => "radio",
        "options" => array("yes" => "Yes", "no" => "Nope"),
        "std" => "no"),

array("name" => __('Header Display Ad','wis'),
        "desc" => __('You can show a display ad in header. Paste the code here for 600px by 60px ad.','wis'),
        "id" => "topbanner",
        "std" => "",
        "type" => "textarea"),

array("name" => __('Hide Footer Navigation Links','wis'),
        "desc" => __('Select to hide the navigation bar in the footer. If you want to customize the footer navigation, go to Menus under the Appearance tab in the dashboard.','wis'),
        "id" => "hide_footer_nav",
        "std" => "",
        "type" => "checkbox"),  

array("name" => __('Twitter ID','wis'),
        "desc" => __('Your Twitter user name, please. It will be shown in the navigation bar. Leaving it blank will keep the Twitter icon supressed.','wis'),
        "id" => "twitterid",
        "type" => "text",
        "std" => ""),       


array( "type" => "close"),

array( "type" => "close")

);


function wis_add_admin() {

global $themename, $shortname, $options;

if ( $_GET['page'] == basename(__FILE__) ) {

    if ( 'save' == $_REQUEST['action'] ) {

        foreach ($options as $value) {
        update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

foreach ($options as $value) {
    if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }

    header("Location: admin.php?page=theme-option.php&saved=true");
die;

} 
else if( 'reset' == $_REQUEST['action'] ) {

    foreach ($options as $value) {
        delete_option( $value['id'] ); }

    header("Location: admin.php?page=theme-option.php&reset=true");
die;

}
}

add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'wis_admin');
}

function wis_add_init() {

$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("functions", $file_dir."/include/functions/functions.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/include/functions/rm_script.js", false, "1.0");

}
function wis_admin() {

global $themename, $shortname, $options;
$i=0;

if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

?>
<div class="wrap rm_wrap">
<h2><?php echo $themename; ?> Settings</h2>

<div class="rm_opts">
<form method="post">
<?php foreach ($options as $value) {
switch ( $value['type'] ) {

case "open":
?>

<?php break;

case "close":
?>

</div>
</div>
<br />


<?php break;

case "title":
?>
<p>To easily use the <?php echo $themename;?> theme, you can use the menu below.</p>


<?php break;

case 'text':
?>

<div class="rm_input rm_text">
    <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
    <input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id'])  ); } else { echo $value['std']; } ?>" />
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>

 </div>
<?php
break;

case 'textarea':
?>

<div class="rm_input rm_textarea">
    <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
    <textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?></textarea>
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>

 </div>

<?php
break;

case 'select':
?>

<div class="rm_input rm_select">
    <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>

<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
        <option <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>
</select>

    <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
</div>
<?php
break;

case "radio":
?>
    <div class="options_input options_select">
        <div class="options_desc"><?php echo $value['desc']; ?></div>
        <span class="labels"><label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label></span>
          <?php foreach ($value['options'] as $key=>$option) {
            $radio_setting = get_option($value['id']);
            if($radio_setting != ''){
                if ($key == get_option($value['id']) ) {
                    $checked = "checked=\"checked\"";
                    } else {
                        $checked = "";
                    }
            }else{
                if($key == $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 } ?>
    </div>

<?php
break;

case "checkbox":
?>

<div class="rm_input rm_checkbox">
    <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>

<?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; ?> />


    <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 </div>
<?php break; 
case "section":

$i++;

?>

<div class="rm_section">
<div class="rm_title"><h3><img src="<?php bloginfo('template_directory')?>/include/functions/images/trans.gif" class="inactive" alt="""><?php echo $value['name']; ?></h3><span class="submit"><input name="save<?php echo $i; ?>" type="submit" value="Save changes" />
</span><div class="clearfix"></div></div>
<div class="rm_options">


<?php break;

}
}
?>

<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
 </div> 


<?php
}
?>
<?php
add_action('admin_init', 'wis_add_init');
add_action('admin_menu', 'wis_add_admin');
?>

I have tested and working fine only you may need to change in this is path of your files (function/function.css etc) Also I have added select, radio, textarea, text and checkbox examples.

18
  • I still can't see the dropdown in theme options page...
    – Arg Geo
    Commented Jun 6, 2012 at 17:56
  • Ah! I posted for template not for option page. Your code was showing template part. anyways let me do it for you. Commented Jun 6, 2012 at 18:08
  • Thank for that piece of code but it has not a save button... also I need it to appear as a sub menu under "Appearance"...
    – Arg Geo
    Commented Jun 7, 2012 at 9:52
  • There is a save button you will find at the top right of the section. To appear into the Appearance menu just register with that instead of registering as new menu Commented Jun 7, 2012 at 10:18
  • I figured it out. Actually what I did, was grabbing the useful piece of code you posted and adding it to my first code (dropdowns and radios). They appear now, also their values are being saved when hitting the "save" button. I don't know how to add these options in my template (in header, I suppose) so the color scheme to be applyed. Same thing for radios, to take effect. Also how to add each section in a different menu tab?
    – Arg Geo
    Commented Jun 7, 2012 at 10:31

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.