Day three of [c] is here! Today we’ll be taking the options we made yesterday, styling them and creating the actual page that displays them in the WordPress backend.
- Day 1: Introduction
- Day 2: Creating the different options
- Day 3: Styling the options page
- Day 4: Implementing the options into a theme
Creating the options page
The first thing we’re going to do today is create the actual page that shows the options, which we can do with the following code, pasted straight after where we left off yesterday, in the functions.php file:
);
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: themes.php?page=theme-options.php&saved=true");
die;
} else if( 'reset' == $_REQUEST['action'] ) {
foreach ($options as $value) {
delete_option( $value['id'] ); }
header("Location: themes.php?page=theme-options.php&reset=true");
die;
} else if ( 'reset_widgets' == $_REQUEST['action'] ) {
$null = null;
update_option('sidebars_widgets',$null);
header("Location: themes.php?page=theme-options.php&reset=true");
die;
}
}
add_theme_page($themename." Options", "Bibliteca Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
}
function mytheme_admin() {
global $themename, $shortname, $options;
if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' '.__('settings saved.','thematic').'</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' '.__('settings reset.','thematic').'</strong></p></div>';
if ( $_REQUEST['reset_widgets'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' '.__('widgets reset.','thematic').'</strong></p></div>';
?>
Styling the elements
The options page is actually just a table, with each element a new row, split into columns, so first, we need to start the table:
<div="wrap">
<?php if ( function_exists('screen_icon') ) screen_icon(); ?>
<h2><?php echo $themename; ?> Options</h2>
<form method="post" action="">
<table class="form-table">
<?php foreach ($options as $value) {
Next, we’re going to style each element, one by one. As I explained yesterday, we can do this once and the PHP will apply the styling multiple times.
I get the following error when I change the WP_DEBUG to true in
if ( ‘save’ == $_REQUEST[‘action’] ) {
Notice: Undefined index: page in C:\wamp\www\wordpress\wp-content\themes\wptheme\includes\theme-options.php on line 214
Any solution for it ?
i forgot to mention that i’ve put the code inside functions.php via the include function, and i have a functions-options.php file which has the code in it.
i’ve checked if that’s the problem, but it didn’t make any different.
is it also becoz of that? is it better to put the code straight into functions.php?
thanks again,
ilan
hello,
thank you very much for really good and useful tutorial 🙂
i have one problem though:
i’ve managed to add the option page with no problem, and it work fine – except of 1 thing:
when i press “save changes” – i get a blank page.
i don’t get back to the same page with a saying that “changes saved” or something – i just get blank page.
it does save the content i’ve put inside the fields and all , so it does work – just this blank page annoys me.
any idea why it happens, and how i can solve it?
thanks in advance,
ilan
Hi, how are you meant to merge the function-options.php file and the funcitons.php?
You just need to copy the contents of one into the other.
[…] Theme Options Page in WordPress is here!Day 1: IntroductionDay 2: Creating the different optionsDay 3: Styling the options pageDay 4: Implementing the options into a themeToday comes the exciting implementation of our options […]
I still cant get the merge files part. Are you merging the two files function-options.php and functions.php using the PHP include statement.
like adding include(‘function-options.php’); at the end of functions.php file?
Thank You.
i’m sorry to bother you again but some of the code that you use (from thematic framework i guess)
is for translating purpose, you should rename it with your own theme
Very true. Thanks for pointing that out.
The download txt link is dead
Hi! I was able to download it. There’s just a missing ‘n’ in the link. It should have been downloads not dowloads.