如何在插件中使用wp_set_password?

时间:2020-02-14 作者:Kirkland

我正在为我的多站点安装开发一个私有插件。我希望用户能够从插件设置页面更新其WP密码。我尝试了一些运气不佳的事情。在下面的代码中do_things 从未调用。当我移除add_action 我通过调用wp_set_password.

任何帮助都将不胜感激。

<?php
/*
  Plugin Name: My Plugin
  Description:
  Author: Me
  Version: 1.0.0
*/
class My_New_Plugin
{
  public function __construct()
  {
    add_action(\'admin_menu\', array($this, \'create_plugin_settings_page\'));
    add_action(\'send_headers\', array($this, \'do_things\'));
  }

  public function create_plugin_settings_page()
  {
    add_submenu_page(\'index.php\', \'My Plugin\', \'My Plugin\', \'read\', \'my_plugin\', array($this, \'plugin_settings_page_content\'), 2);
  }

  public function do_things() {
    wp_set_password(\'test\', get_current_user_id());

    $user = wp_signon(array(
      \'user_login\' => \'deleteme\',
      \'user_password\' => \'test\',
      \'remember\' => true
    ), false);

    if (is_wp_error($user)) {
      echo $user->get_error_message();
    } else {
      wp_clear_auth_cookie();
      do_action(\'wp_login\', $user->ID);
      wp_set_current_user($user->ID);
      wp_set_auth_cookie($user->ID, true);
      $redirect_to = $_SERVER[\'REQUEST_URI\'];
      wp_safe_redirect($redirect_to);
      exit;
    }
  }

  public function plugin_settings_page_content()
  {
    echo \'<div>stuff</div>\';
  }
}
new My_New_Plugin();

1 个回复
最合适的回答,由SO网友:Kirkland 整理而成

我用错了动作钩。已替换send_headers 具有init 这是可行的。

Wrong

add_action(\'send_headers\', array($this, \'do_things\'));

Right

add_action(\'init\', array($this, \'do_things\'));