0

My theme options save routine is below. I'm finding that if the $value['id'] being passed from my options array has a period in it, the data does not get passed and the options appear to break at that point. Should I opt for another character or is there a workaround for using the period character in an option name?

For example, this works fine:

 "id" => "myTheme_color|sidebar",

but this does not (no data is passed for the value):

 "id" => "myTheme_color.sidebar",

The save function is:

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'], stripslashes($_REQUEST[$value['id']]) ); 
        }
        foreach ($options as $value) 
        {
            if( isset( $_REQUEST[ $value['id'] ] ) ) 
            {
                update_option( $value['id'], stripslashes($_REQUEST[ $value['id'] ])  ); 
            echo $value['id'].": ".$_REQUEST[$value['id']]."<br>";
            echo $value['id'].": ".stripslashes($_REQUEST[$value['id']])."<br>";
            } 
        }
    }
}
2
  • You're doing two times the same thing (2xforeach) and the previous variant might just overwrite the option if the not set in $_REQUEST. Probably this is the issue?
    – hakre
    Commented Nov 23, 2010 at 23:13
  • Not sure why I had that there, but I've removed it and it does no good. Apparently you can't use periods in options names when using a for loop on the options array at save time.
    – Scott B
    Commented Nov 26, 2010 at 4:10

2 Answers 2

1

Dots and spaces are replaced by PHP for array indexes via POST and GET. That may cause your problem.

0
0

I am not sure what the issue is here, this test code works fine for me:

$options = array( 'test_option' => array('id' => 'myTheme_color.sidebar') );
$test = 'test';
var_dump($options);
foreach ($options as $value) {
    var_dump($value['id']);
    update_option( $value['id'], $test );
}

From quick look at source code the most likely point for update_option to fail is if SQL query to insert it fails.

My suggestions are to run $wpdb->print_error() after operation to check for failed query and hook into update_option_{$option} and updated_option actions to see if they are firing (source).

1
  • And if you have not done it, add `define('WP_DEBUG', TRUE); on the testbed. This might reveal more information with $wpdb as well.
    – hakre
    Commented Nov 26, 2010 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.