Problem In Retrieving Wordpress Themes Option Variable Values


#1

Hi,
Hope u all will be fine i’m new here, and i am developing a wp theme, basic design and functions for this theme is almost complete. But i want to add option page for theme in admin panel. For this i am using code something like this:

<?php $themename = "Nettuts"; $shortname = "nt"; $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"); $options = array ( array( "name" => $themename." Options", "type" => "title"), array( "name" => "General", "type" => "section"), array( "type" => "open"), array( "name" => "Colour Scheme", "desc" => "Select the colour scheme for the theme", "id" => $shortname."_color_scheme", "type" => "select", "options" => array("blue", "red", "green"), "std" => "blue"), array( "name" => "Logo URL", "desc" => "Enter the link to your logo image", "id" => $shortname."_logo", "type" => "text", "std" => ""), array( "name" => "Custom CSS", "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}", "id" => $shortname."_custom_css", "type" => "textarea", "std" => ""), array( "type" => "close"), array( "name" => "Homepage", "type" => "section"), array( "type" => "open"), array( "name" => "Homepage header image", "desc" => "Enter the link to an image used for the homepage header.", "id" => $shortname."_header_img", "type" => "text", "std" => ""), array( "name" => "Homepage featured category", "desc" => "Choose a category from which featured posts are drawn", "id" => $shortname."_feat_cat", "type" => "select", "options" => $wp_cats, "std" => "Choose a category"), array( "type" => "close"), array( "name" => "Footer", "type" => "section"), array( "type" => "open"), array( "name" => "Footer copyright text", "desc" => "Enter text used in the right side of the footer. It can be HTML", "id" => $shortname."_footer_text", "type" => "text", "std" => ""), array( "name" => "Google Analytics Code", "desc" => "You can paste your Google Analytics or other tracking code in this box. This will be automatically added to the footer.", "id" => $shortname."_ga_code", "type" => "textarea", "std" => ""), array( "name" => "Custom Favicon", "desc" => "A favicon is a 16x16 pixel icon that represents your site; paste the URL to a .ico image that you want to use as the image", "id" => $shortname."_favicon", "type" => "text", "std" => get_bloginfo('url') ."/favicon.ico"), array( "name" => "Feedburner URL", "desc" => "Feedburner is a Google service that takes care of your RSS feed. Paste your Feedburner URL here to let readers see it in your website", "id" => $shortname."_feedburner", "type" => "text", "std" => get_bloginfo('rss2_url')), array( "type" => "close") ); function mytheme_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=functions.php&saved=true"); die; } else if( 'reset' == $_REQUEST['action'] ) { foreach ($options as $value) { delete_option( $value['id'] ); } header("Location: admin.php?page=functions.php&reset=true"); die; } } add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'mytheme_admin'); } function mytheme_add_init() { $file_dir=get_bloginfo('template_directory'); wp_enqueue_style("functions", $file_dir."/functions/functions.css", false, "1.0", "all"); wp_enqueue_script("rm_script", $file_dir."/functions/rm_script.js", false, "1.0"); } function mytheme_admin() { global $themename, $shortname, $options; $i=0; if ( $_REQUEST['saved'] ) echo '

'.$themename.' settings saved.

'; if ( $_REQUEST['reset'] ) echo '

'.$themename.' settings reset.

'; ?>

<?php echo $themename; ?> Settings

<?php foreach ($options as $value) { switch ( $value['type'] ) {

case “open”:
?>

<?php break; case "close": ?>

<?php break; case "title": ?>

To easily use the <?php echo $themename;?> theme, you can use the menu below.

<?php break; case 'text': ?>
<?php echo $value['name']; ?> " /> <?php echo $value['desc']; ?>
<?php break;

case ‘textarea’:
?>

<?php echo $value['name']; ?> <?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?> <?php echo $value['desc']; ?>
<?php break; case 'select': ?>
<?php echo $value['name']; ?> <?php foreach ($value['options'] as $option) { ?> ><?php echo $option; ?><?php } ?>
<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
<?php break;

case “checkbox”:
?>

<?php echo $value['name']; ?> <?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>
<?php break; case "section":

$i++;

?>

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

<?php break; } } ?>

<?php } ?> <?php add_action('admin_init', 'mytheme_add_init'); add_action('admin_menu', 'mytheme_add_admin'); ?>

By Using this code admin panel is working fine, when we visit ‘wp-admin/options.php’ it shows all custom function we created by using above code with their saved values. But when i try to use these function in my theme file after adding this code in header.php these functions does not work at all:

<? global $options; foreach ($options as $value) { if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } } ?>

I’m trying to resolve this issue from many hours but i cannot understand what is the problem. Please if any body know the solution, tell here in reply.

Regards

ps: sorry for my bad english i’m from Asia


#2

One thing more for information i’m testing this code in localhost.


#3

Try pasting your code onto a website like pastebin.com - we cannot read that code you pasted here. The forum doesn’t handle it properly. (or mods change code to pre tag.)

To set a custom variable in your theme admin page, use this code to save the values to wordpress:

update_option("option_key","Option Value");
update_option("something",$_POST['something']);

To get those variables back in your script, use something like this:

echo get_option("option_key","Some Default Value");
echo get_option("something","Another default value");

I suggest you read some articles on creating wordpress option pages.

There’s one over on the blog:

Dave


#4

This code:

<? global $options; foreach ($options as $value) { if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } } ?> 

Is of no use.

Just use get_option(‘option_name’) to fetch an option.


#5

@dtbaker thanks for answering, problem has now been solved by rohan.

@_rohan thank u very much you made my day. Now it working as a charm. Now please tell me i want to only echo this option in my theme if it has some value saved in it, i’m using following code but it is not working:

<? if (get_option('nt_footer_text') == true) { ?>

<?php echo get_option('nt_footer_text'); ?>

<? } else { ?>

Please enter fotter text here from the Admin Panel.

<? } ?>

Thanks again for help.


#6

It should probably be:

<? if (get_option(‘nt_footer_text’) != "") { ?>

if it is a textarea field


#7
@dtbaker thanks for answering, problem has now been solved by rohan.

@_rohan thank u very much you made my day. Now it working as a charm. Now please tell me i want to only echo this option in my theme if it has some value saved in it, i’m using following code but it is not working:

<? if (get_option('nt_footer_text') == true) { ?>

<?php echo get_option('nt_footer_text'); ?>

<? } else { ?>

Please enter fotter text here from the Admin Panel.

<? } ?>

Thanks again for help.

Instead of if (get_option(‘nt_footer_text’) == true, use if (get_option(‘nt_footer_text’, “”) != “”)


#8
It should probably be: <? if (get_option(‘nt_footer_text’) != "") { ?>

if it is a textarea field

Good answer, but if the option doesn’t exist, the return value is boolean false. So, you can use get_option(‘nt_footer_text’, “”) which says the default not-exist return value should be “” instead of false.


#9

Thanks _Rohan and Tonyc for replies but unfortunately both codes r not working.