0

i want to modify my options data after save. Therefore i want to hook into "updated_option". This works without problem.

I create a new option called "my_update_option" and want also hook into "updated_option_my_update_option" but this won't work.

Did anyone know whats wrong in my code below?

class MyClass {

public function __construct()
{
    add_action('admin_init', [$this, 'theme_init']);
    add_action('admin_menu', [$this, 'theme_menu']);

    add_action('updated_option', [$this, 'save_options'], 10, 3);
    add_action('updated_option_my_update_option', [$this, 'save_my_update_option'], 10, 3);
}

public function theme_init()
{
    register_setting('my_update_option', 'theme_options', 'lapocus_validate_options', 'checkOption');
}

public function theme_menu()
{
    add_menu_page('Main Page', 'Main Page', 'my_update_option', 'menu-slug', [$this, 'main_page'], 'dashicons-admin-network', 2);
    add_submenu_page('menu-slug', 'Sub Page', 'Sub Page','my_update_option', 'menu-slug' . '-subpage-online-form', [$this, 'subpage_online_form']);
}

public function save_my_update_option($oldvalue, $value, $option) {
   // do something
}

1 Answer 1

1

This is no updated_option_{$option} filter. You'd have to use update_option_{$option} instead:

    /**
     * Fires after the value of a specific option has been successfully updated.
     *
     * The dynamic portion of the hook name, `$option`, refers to the option name.
     *
     * @since 2.0.1
     * @since 4.4.0 The `$option` parameter was added.
     *
     * @param mixed  $old_value The old option value.
     * @param mixed  $value     The new option value.
     * @param string $option    Option name.
     */
    do_action( "update_option_{$option}", $old_value, $value, $option );

Don't forget to remove your filter before if you save the option again:

class MyClass {

public function __construct()
{
    add_action('admin_init', [$this, 'theme_init']);
    add_action('admin_menu', [$this, 'theme_menu']);

    add_action('updated_option', [$this, 'save_options'], 10, 3);
    add_action('update_option_my_update_option', [$this, 'save_my_update_option'], 10, 3);
}

public function theme_init()
{
    register_setting('my_update_option', 'theme_options', 'lapocus_validate_options', 'checkOption');
}

public function theme_menu()
{
    add_menu_page('Main Page', 'Main Page', 'my_update_option', 'menu-slug', [$this, 'main_page'], 'dashicons-admin-network', 2);
    add_submenu_page('menu-slug', 'Sub Page', 'Sub Page','my_update_option', 'menu-slug' . '-subpage-online-form', [$this, 'subpage_online_form']);
}

public function save_my_update_option($oldvalue, $value, $option) {
  // Begin prevent infinite loop
  remove_action('update_option_my_update_option', [$this, 'save_my_update_option'], 10);

  // do something
  $value = $this->change_value($old_value, $value);

  // save the option again
  update_option('my_update_option', $value);

  // End prevent infinite loop
  add_action('update_option_my_update_option', [$this, 'save_my_update_option'], 10, 3);
}

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.