Php 在wordpress中加载头文件之前执行add_filter函数
我已经创建了一个与fb登录集成的登录页面。当用户单击忘记密码时,它将重定向到一个页面,要求输入您的用户名。单击“提交”按钮时,会生成一条错误消息,说明您已使用fb登录,请更改fb密码 为了实现这一点,我使用了add_过滤器“allow_password_reset hook”。但无论写入什么,都会在加载头文件之前执行它,并且输出显示在头文件的顶部。我已经尝试了我所知道的一切,但问题仍然存在。这是我的密码:Php 在wordpress中加载头文件之前执行add_filter函数,php,wordpress,add-filter,Php,Wordpress,Add Filter,我已经创建了一个与fb登录集成的登录页面。当用户单击忘记密码时,它将重定向到一个页面,要求输入您的用户名。单击“提交”按钮时,会生成一条错误消息,说明您已使用fb登录,请更改fb密码 为了实现这一点,我使用了add_过滤器“allow_password_reset hook”。但无论写入什么,都会在加载头文件之前执行它,并且输出显示在头文件的顶部。我已经尝试了我所知道的一切,但问题仍然存在。这是我的密码: function se_deny_password_reset() { $allo
function se_deny_password_reset() {
$allow = false;
showMessage("Error", true);
return $allow;
}
add_filter( 'allow_password_reset', 'se_deny_password_reset', 10, 2 );
function showMessage($message, $errormsg = false)
{
echo '<p><strong>' . $message . '</strong></p></div>';
}
函数seu_deny_password_reset(){
$allow=false;
showMessage(“错误”,true);
返回$allow;
}
添加过滤器(“允许密码重置”、“拒绝密码重置”,10,2);
函数showMessage($message,$errormsg=false)
{
回显“”.$message.“”;
}
注意:
您的问题没有明确显示消息的位置,因此这只是一种模式,您可以使用它来跟踪$allow
变量的状态,以便依次显示您认为合适的消息:
(使用此代码而不是现有的功能)
是的,该过滤器在WP运行的早期运行。你不应该在那一点上重复任何东西。只要返回FALSE。如果您出于某种原因需要回显消息,那么您应该跟踪
$allow
的状态,然后在另一个钩子中您可以回显消息。谢谢您的cmnt,但我感到困惑!v如何跟踪变量($allow)的状态。
class MyPasswordClass {
/**
* Track the state of $allow.
* Change if desired in a class function like so: $this->allow = TRUE;
*
* @var bool
*/
private $allow = FALSE;
/**
* Constructor.
* Set up the action / filter hooks.
*/
public function __construct() {
add_filter( 'allow_password_reset', array( $this, 'allow_password_reset' ), 10, 2 );
add_filter( 'retrieve_password_message', array( $this, 'password_message' ), 10, 4 );
}
/**
* Hooks into WP 'allow_password_reset' filter.
*
* @param bool $allow
* @param int $user_id
*
* @return bool
*/
public function allow_password_reset( $allow, $user_id ) {
// If desired, change permission. Example:
// $this->allow = TRUE;
return $this->allow;
}
/**
* Hooks into WP 'retrieve_password_message' filter.
* Message that displays when user attempts to reset password.
*
* @param string $message
* @param string $key
* @param string $user_login
* @param object $user_data
*
* @return string
*/
public function password_message( $message, $key, $user_login, $user_data ) {
$message = 'My custom error message.';
return $message;
}
}
// Instantiate the class so that the filters are set up / used
new MyPasswordClass();