Is there anyway to get notified when someone logs on to the site or is even just browsing the site? I would like to receive email notifications when this happens but I cannot seem to find anywhere to activate this feature.
3 Answers
You can trigger wp_mail()
to send you an email when you have a visitor, although you may end up getting flooded with emails since it'll email you every time a page is viewed.
/**
* Send an email with every visit.
*/
function wpse113069_email_me_every_visit() {
wp_mail( '[email protected]', 'You had a visitor...', 'Yea, someone was on your site!' );
}
add_action( 'wp', 'wpse113069_email_me_every_visit' );
You can also use is_user_logged_in()
to extend this to include the page/post, the date & time of the visit, etc., or to customize the notification based on if someone is logged in or is an anonymous visitor.
/**
* Send a different email based on if a visitor is signed into the site.
*/
function wpse113069_email_me() {
$msg = 'An anonymous visitor visited!';
if ( is_user_logged_in() ) {
$msg = 'A member visited!';
}
wp_mail( '[email protected]', 'You had a visitor...', $msg );
}
add_action( 'wp', 'wpse113069_email_me' );
-
Won't this send an email every page load? I bet there is a better hook... Commented May 25, 2017 at 9:00
-
Yes it does, which is why I noted that in my answer. The OP asked about signing in or "just browsing the site." Commented May 25, 2017 at 11:42
function whoisWatchingMe(){
$email = "[email protected]";
wp_mail( $email, 'You had a visitor...', 'Woopty do!' );
}
add_action( 'wp_login', 'whoisWatchingMe' );
function set_mail_here() {
return 'text/html';
}
add_action( 'wp_login', 'notice_here' );
function notice_here() {
if( is_user_logged_in() ) {
return;
}
$to = '[email protected]';
$subject = 'Message';
$message = '<h1>Someone logged in.</h1>';
add_filter( 'wp_mail_content_type', 'set_mail_here' );
wp_mail( $to, $subject, $message );
remove_filter( 'wp_mail_content_type', 'set_mail_here' );
}
source: https://premium.wpmudev.org/blog/email-notifications-wordpress/