0

I have the following bit of code (note, I put the mail() function in only to see what the values are):

add_action('init','system_init_actions');
function system_init_actions() {
    add_action('update_option_system_number','system_save_number');
}

function system_save_number($oldvalue,$newvalue) {
    global $wpdb;
    global $table_prefix;
    mail('my@email.com','here',print_r($oldvalue,true) . "\n\n" . print_r($newvalue,true));
    $wpdb->update($table_prefix . "system_config",['status'=>'deactivated'],['number'=>$oldvalue]);
    $wpdb->insert($table_prefix . "system_config",['number'=> $newvalue,'created'=>date('Y-m-d H:i:s'),'status'=>'pending']);
}

In my plugin options page, I have an option named system_number. When I save that system number, I get an email (again, only to see the values) which means it is calling the function.

$newvalue is passed with no problem, but the function is not receiving $oldvalue. I had a look at the update_option() method in wp-includes/option.php and it fires do_action ( "update_option_{$option}", mixed $old_value, mixed $value ), which should pass the old value.

Why is it not giving me that value?

2

You should use pre_update_option_{option_name} filter.

Example:-

function system_save_number( $new_value, $old_value ) {
    mail('my@email.com','here',print_r($old_value ,true) . "\n\n" . print_r($new_value,true));
    return $new_value;
}

function system_init_actions() {
    add_filter( 'pre_update_option_system_number', 'system_save_number', 10, 2 );
}

add_action( 'init', 'system_init_actions' );
  • Out of curiosity, what is update_option_{$option}'s purpose then? – Albert Jun 24 '15 at 18:54
  • For this codex documentation is not available link – WisdmLabs Jun 24 '15 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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