我的网站在 WordPress 上运行,包括一个像这样工作的帐户激活系统:
- 用户创建帐户,立即登录并显示主页。
- 一个函数检查数据库以查看帐户是否已激活,如果没有,则在主页上显示一条消息,要求点击验证电子邮件中的链接。
- 用户打开他们的电子邮件并点击激活链接。 (例如:https://example.com/?action=activate&user=swen&key=1234)
- 打开此页面时,函数会检查是否设置了
action
参数并运行帐户激活函数,然后将帐户更新为“已激活”。 - WP的其余部分都执行完了,但是检查账户激活的功能仍然显示账户没有激活。
- 浏览到https://example.com再次没有查询变量,消息消失,表明上面的示例 URL 实际上确实可以激活帐户。
是否有可能将数据写入数据库与检索相同更新数据之间的时间太短?
更新:
想知道 MySQL 是否在同一页面加载中缓存数据。我尝试在检索更新的用户角色之前刷新 WordPress 缓存,但没有成功。
检查 URL 并运行 activate_user
函数的操作:
我尝试使用 setup_theme
Hook 尽早执行此操作,以确保它在加载主题之前运行。
add_action('setup_theme', 'ns_activate_user_action');
function ns_activate_user_action() {
// Check if all action parameters are present
if ( !ns_validate_action('activate', array('user', 'key')) ) {
return;
}
// Activate user by key, runs the "activate_user" function
$activate_user = activate_user_by_key($_GET['user'], $_GET['key']);
// At this point the user role should have changed
// and the account be activated
}
激活账户的函数:
function activate_user($user) {
// ...
$user->add_role( 'member' );
$user->remove_role( 'member_pending' );
// ...
return $user;
}
检查账户是否激活的函数:
function is_user_activated($user) {
// ...
if ( in_array( 'member_pending', (array) $user->roles ) ) {
return false;
}
return true;
}
自定义操作显示激活消息,在主题文件中使用 do_action()
调用:
add_action('ns_after_header', 'ns_activate_account_notice_front_page');
function ns_activate_account_notice_front_page() {
if ( is_user_logged_in() && is_front_page() && !is_user_activated() ) {
// ... Echos message asking user to click activation link in email
// Checking the user role here with wp_get_current_user()->roles
// still shows that the user is in the "member_pending" role (inactive)
}
}
最佳答案
我会尝试使用 add_action 优先级。由于您没有在 add_action 函数中指定它们,因此它们都默认为十 (10)。
https://developer.wordpress.org/reference/functions/add_action/
这似乎是他们的完美用例
关于php - 写入数据库,并在同一个 PHP 页面加载中检索更新的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51844677/