admin管理员组文章数量:1392101
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 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();
Share
Improve this question
edited Feb 14, 2020 at 19:24
Kirkland
asked Feb 14, 2020 at 5:44
KirklandKirkland
3082 gold badges3 silver badges14 bronze badges
1 Answer
Reset to default 1I 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'));
本文标签: customizationHow to use wpsetpassword in a plugin
版权声明:本文标题:customization - How to use wp_set_password in a plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745552a2622855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论