OK I’ve fixed the huge options page from coming up by adding <?php settings_fields( 'sa_theme_options' ); ?> but my problem lies with storing the options in the database.
I borrowed some code on this page that I believe stores all the options in one database entry in the form of an array.
Meanwhile, my options page assigns an id to each option with the intention of storing each option as 1 database entry.
Just not sure how to sort this out…
OK sorry to keep bumping but I’m slowly picking up things:
The purpose of the register_setting() function is clearer to me now. I’m just not quite sure how to give it the values to store in the database.
E.g. if I have the $option_name parameter (parameter 2) set to sa_options I have then, in my form, set one of my text fields to have name="sa_options[sa_headerfontsize]".
Am I right in thinking that register_setting() should save whatever in typed in the text field in the sa_options array as so?:
sa_options["sa_headerfontsize"] = "whatever is typed";
I tried this but using phpmyadmin, there is a “sa_options” database entry in the wp_options table but it doesn’t have anything in it.
You may find this tutorial helpful.
Thanks I have had a read over it but it is very long and complicated so I can’t find the specific bit I’m looking for.
Cna you explain how the fields in the form are put into the database. Because I don’t think register_setting() is getting the entries.
Here is the html that my sa_theme_options_page() and sa_options_table() generate: http://pastebin.com/NB2bTuWZ
The first thing I notice is, you don’t have a validation callback. You’ve specified one in your register_setting() call, but you’ve not defined it anywhere in your pastebin code. (You have left function sa_validate_options() {} blank. Since the form $input is passed to this function, at the very least, you need to return it, e.g.:
function sa_validate_options( $input ) {
return $input;
}
Beyond that, you’ll have to step through your code, and find out what’s going wrong.
(Calling var_dump( $input ); die;inside ofsa_validate_options()` is helpful to see what data are being passed into the function.)
Great thanks for the reply; I’m now getting somewhere.
Checking my database using phpmyadmin, I can see that it is now storing the options when I submit the form. This is having fixed my validation function as you described.
(I will implement some validation soon but I just want the concept to work first)
i think you’ve answered all my problems for now, but I’ll be back if/when I stumble again.
Thanks again..
@chip: Perhaps you could add a link for your tutorial to the Codex someplace? I think it would be a great addition.
OK I’ve hit a stumbling block.
Most is working but I’m having trouble calling the options into the theme:
I’m storing all the options in an array under the database entry called sa_options.
In my functions.php I have defined the array $sa_settings = get_option('sa_options'); to allow me to call the options onto any theme page.
This mostly works but does not work for the header.php.
if I put print_r ($sa_settings) in header.php (within the actual body so I can actually read whatever is output on the page) nothing is output.
However, if I put the same function in index.php it prints the contents of the array.
It seems as though wordpress calls header.php before defining $sa_settings (which is defined in functions.php). Any idea how to ensure the $sa_settings variable is defined inside header.php aside from putting it there?
actually, just putting $sa_settings = get_option('sa_options'); at the top of header.php is probably the best way isn’t it?
actually, just putting $sa_settings = get_option(‘sa_options’); at the top of header.php is probably the best way isn’t it?
You got it!
I’m thinking of playing around with hooking something into wp_head, just because I like to keep my header.php as clean as possible. But in any case, yes: you do need to output $sa_settings = get_option('sa_options'); somewhere in the template directly.
OK I’ve put it in header.php and stuff in the header works but now the $sa_settings seems to be null within index.php.
Is is OK to put it in functions.php too. Or how do I make the variable available everywhere?
First, you should probably globalize it:
global $sa_settings = get_option('sa_options');
Second, I don’t know for certain. I’ve been trying to figure out the definitive answer to this one. I think the answer is that, if it’s called within a function, it’s not going to be available outside of the function.
You might try adding the call to the top of your master template files (index.php, page.php, etc.), before the <?php get_header; ?> call?
It’s not called within a function.
For simplicity I’ve put it in header.php and functions.php like so:
if (!isset($sa_settings)){
$sa_settings = get_option( 'sa_options' );
}
For simplicity I’ve put it in header.php and functions.php like so:
Are you globalizing it everywhere it’s called?