带有条件标记的WP_LOGIN操作挂钩

时间:2017-07-31 作者:Mario gasparella

我尝试创建一个简单的函数,在指定用户登录时接收电子邮件,我尝试使用$current\\u user->ID和wp\\u get\\u current\\u user()但不起作用。这是我的代码:

function InvioMail() {
     global $current_user;
   $ID = $current_user->ID;
    $user = wp_get_current_user();
  $name = $user->user_login;

    if ($name == \'piero\') {

        $to = \'example@mail.com\';
        $subject = \'test su action hook wp_login\';
        $body = \'test su action hook pwp_login\';
        $headers = array(\'Content-Type: text/html; charset=UTF-8\',\'From: My Site Name <support@example.com\');

        wp_mail( $to, $subject, $body, $headers );
    }
}

add_action( \'wp_login\', \'InvioMail\');
没有if条件工作属性,但当我尝试if nothing时。

我错在哪里?

2 个回复
最合适的回答,由SO网友:Cesar Henrique Damascena 整理而成

这应该适合您,请确保user_loginuser_id 与条件中使用的字符串匹配。试着做一个var_dump($user_login);

关于您正在使用的函数,您不需要获取global 或致电get_current_user();, 因为您调用的操作已经向函数传递了两个参数,第一个是string 使用$user_login, 第二个是WP_User object 当前登录的用户。

要使用它,只需将代码更改为:

function InvioMail($user_login, $user) {

  if ($user_login == \'piero\') {

    $to = \'example@mail.com\';
    $subject = \'test su action hook wp_login\';
    $body = \'test su action hook pwp_login\';
    $headers = array(\'Content-Type: text/html; charset=UTF-8\',\'From: My Site Name <support@example.com\');

    wp_mail( $to, $subject, $body, $headers );
  }
}

add_action( \'wp_login\', \'InvioMail\', 10, 2);
要了解有关此挂钩的更多信息,请检查Docs.

SO网友:Nuno Sarmento

请尝试下面的代码。

function InvioMail() {
  global $current_user;

  if ($current_user->ID == 1){ // change id 1 to your user id 

    $to = \'example@mail.com\';
    $subject = \'test su action hook wp_login\';
    $body = \'test su action hook pwp_login\';
    $headers = array(\'Content-Type: text/html; charset=UTF-8\',\'From: My 
    Site Name <support@example.com\');

    wp_mail( $to, $subject, $body, $headers );
   }
}

add_action( \'wp_login\', \'InvioMail\');

结束

相关推荐

Hooks for Links Box

Possible Duplicate:Getting archive pages in WP's AJAX internal link finder? 新的有挂钩吗internal links box 创建于WP 3.1? 我正在尝试在插件中修改它。