[wp-hackers] Issues with register_setting/add_settings_field
Milan Dinić
liste at srpski.biz
Mon Aug 10 15:43:59 UTC 2009
(cross-posted from support forum)
I'm working on some hack (plugin) that should place checkbox on Permalink
Settings page in administration. Problem is that I can't save value in
database, no matter if field is checked or not.
Everything else works correct, i.e. if I manually change value of option in
database then checkbox is changed too, but I can't save after clicking on
Permalink Settings page.
Here is code:
//function to add field on Permalink Settings page
//based on http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function add_ser_cyr_to_lat_slug_settings_field() {
// The fields are:
//the id the form field will use
//name to display on the page
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('ser_cyr_to_lat_slug' , 'Пресловљавање подлошка' ,
'ser_cyr_to_lat_slug_field_callback' , 'permalink' , 'optional');
//register the setting to make sure it gets checked
register_setting('permalink','ser_cyr_to_lat_slug',
'ser_cyr_to_lat_slug_validate');
}
//function for printing field on Permalink Settings page
//based on http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function ser_cyr_to_lat_slug_field_callback() {
global $ser_cyr_to_lat_slug, $broj;
//print checkox ?>
<label for="ser_cyr_to_lat_slug"><input id="ser_cyr_to_lat_slug"
name="ser_cyr_to_lat_slug" type="checkbox" value="1"
<?php checked('1', get_option('ser_cyr_to_lat_slug')); ?>
/> Преслови српска ћирилична у енглеска латинична слова у
пермалинковима</label>
<?php }
// Sanitize and validate input.
//based on http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function ser_cyr_to_lat_slug_validate($input) {
// Our first value is either 0 or 1
$input = ( $input == 1 ? 1 : 0 );
return $input;
}
//add action so that functions could actually work
add_action('admin_init', 'add_ser_cyr_to_lat_slug_settings_field');
As you can see, this script is based on Ozh's tip. When I first time tried
this tip, I just copied his functions and that worked without problem. Later
when I renamed functions and variables, this didn't work.
Here is code that worked:
add_action('admin_init', 'add_my_settings_field');
function add_my_settings_field(){
// The fields are:
//the id the form field will use
//name to display on the page
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('my_field_id' , 'My Field Caption' ,
'my_field_callback' , 'writing' , 'default');
//register the setting to make sure it gets checked
register_setting('writing','my_field_id', 'ozh_sampleoptions_validate');
}
$my_field_id = get_option('my_field_id');
$broj = 1;
//if (!$my_field_id) {
function my_field_callback(){
global $my_field_id, $broj;
//echo out the text field, drop down, or other type of field. ?>
<input id="my_field_id" name="my_field_id" type="checkbox" value="1"
<?php checked('1', get_option('my_field_id')); ?>
/>
<?php }
// Sanitize and validate input. Accepts an array, return a sanitized array.
function ozh_sampleoptions_validate($input) {
// Our first value is either 0 or 1
$input = ( $input == 1 ? 1 : 0 );
return $input;
}
I can't see where I made mistake. I spent a lot of time trying to fix this
but without success.
If you could see what is problem, please write it.
Thanks in advance.
More information about the wp-hackers
mailing list