171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

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:

&lt;?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?&gt;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 '<div class="updated fade" id="message"><p><strong>'.$themename.' settings saved.</strong></p></div>'; if ( $_REQUEST['reset'] ) echo '<div class="updated fade" id="message"><p><strong>'.$themename.' settings reset.</strong></p></div>'; ?&gt; <div class="wrap rm_wrap"> <h2>&lt;?php echo $themename; ?&gt; Settings</h2> <div class="rm_opts"> <form method="post"> &lt;?php foreach ($options as $value) { switch ( $value['type'] ) { case "open": ?&gt; &lt;?php break; case "close": ?&gt; </form></div> </div> <br /> &lt;?php break; case "title": ?&gt; <p>To easily use the &lt;?php echo $themename;?&gt; theme, you can use the menu below.</p> &lt;?php break; case 'text': ?&gt; <div class="rm_input rm_text"> <label for="&lt;?php echo $value['id']; ?&gt;">&lt;?php echo $value['name']; ?&gt;</label> <input name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;" type="&lt;?php echo $value['type']; ?&gt;" value="&lt;?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?&gt;" /> <small>&lt;?php echo $value['desc']; ?&gt;</small><div class="clearfix"></div> </div> &lt;?php break; case 'textarea': ?&gt; <div class="rm_input rm_textarea"> <label for="&lt;?php echo $value['id']; ?&gt;">&lt;?php echo $value['name']; ?&gt;</label> <textarea name="&lt;?php echo $value['id']; ?&gt;" type="&lt;?php echo $value['type']; ?&gt;" rows="" cols="">&lt;?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?&gt;</textarea> <small>&lt;?php echo $value['desc']; ?&gt;</small><div class="clearfix"></div> </div> &lt;?php break; case 'select': ?&gt; <div class="rm_input rm_select"> <label for="&lt;?php echo $value['id']; ?&gt;">&lt;?php echo $value['name']; ?&gt;</label> <select name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;"> &lt;?php foreach ($value['options'] as $option) { ?&gt; <option &lt;?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?&gt;>&lt;?php echo $option; ?&gt;&lt;?php } ?&gt; </select> <small>&lt;?php echo $value['desc']; ?&gt;</small><div class="clearfix"></div> </div> &lt;?php break; case "checkbox": ?&gt; <div class="rm_input rm_checkbox"> <label for="&lt;?php echo $value['id']; ?&gt;">&lt;?php echo $value['name']; ?&gt;</label> &lt;?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?&gt; <input type="checkbox" name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;" value="true" &lt;?php echo $checked; ?&gt; /> <small>&lt;?php echo $value['desc']; ?&gt;</small><div class="clearfix"></div> </div> &lt;?php break; case "section": $i++; ?&gt; <div class="rm_section"> <div class="rm_title"><h3><img src="&lt;?php bloginfo('template_directory')?&gt;/functions/images/trans.gif" class="inactive" alt=""">&lt;?php echo $value['name']; ?&gt;</h3><span class="submit"><input name="save&lt;?php echo $i; ?&gt;" type="submit" value="Save changes" /> </span><div class="clearfix"></div></div> <div class="rm_options"> &lt;?php break; } } ?&gt; <input name="action" type="hidden" value="save" /> <form method="post"> <p class="submit"> <input name="reset" type="submit" value="Reset" /> <input name="action" type="hidden" value="reset" /> </p> </form> </div> &lt;?php } ?&gt; &lt;?php add_action('admin_init', 'mytheme_add_init'); add_action('admin_menu', 'mytheme_add_admin'); ?&gt; </div>

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

171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

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

3047 posts
  • Located in Australia
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
  • Helped several times protecting Envato Market against copyright violations
+16 more
dtbaker Volunteer moderator says

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:

http://blog.themeforest.net/wordpress/create-an-options-page-for-your-wordpress-theme/

Dave

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says

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.

171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

@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.

164 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
tonyc says

It should probably be:

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

if it is a textarea field

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says
@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', "") != "")

374 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan says
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.

171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

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

by
by
by
by
by
by