0

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.

1
  • 4
    it is possible but this is not a good idea, send email to you on each visit, use google analytics tracking or any other visitor tracking plugin like this or this or this or this
    – Anjum
    Commented Sep 5, 2013 at 16:20

3 Answers 3

2

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' );
2
  • 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
1
function whoisWatchingMe(){
    $email = "[email protected]";
    wp_mail( $email, 'You had a visitor...', 'Woopty do!' );
}

add_action( 'wp_login', 'whoisWatchingMe' );
0
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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.