I am developing a private plugin for my multisite installation. I want the user to be able to update their WP password from the plugin settings page. I've tried a few things without luck. In the code below do_things is never called. When I remove the add_action I get the "Headers already sent" error by calling wp_set_password.

Any help would be greatly appreciated.

<?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();
有帮助吗?

解决方案

I was using the wrong action hook. Replaced send_headers with init and it works.

Wrong

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

Right

add_action('init', array($this, 'do_things'));
许可以下: CC-BY-SA归因
scroll top