最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hook to add_action('updated_option_my_option', [$this, 'save_data'], 10, 3]);

programmeradmin6浏览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
}

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
}
Share Improve this question asked Apr 12, 2022 at 4:52 pennylanepennylane 1211 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论