WordPress : Send Mail to Admin when user comes to 404 page

Sending mails to admin is very easy in wordpress by using a inbuild wordpress function wp_mail.
But if you want to send a mail to wordpress admin when user comes to 404 page then you can use below simple code.

Sending mail when user comes to 404 page in wordpress
This code can be added in your wordpress theme’s functions.php file or in custom plugin. You need to write a custom plugin for the same. Please let me know if you need any help to write a custom plugin. As i am using a custom plugin for my wordpress websites and that custom plugin have lots of functions and wordpress hooks that makes my life easy and simple.

Below script checks the referring link of your website, and then sends an email to the webmaster/wordpress admin.

if (!function_exists('email_admin')){
	function email_admin(){
		$message = "";
		if(isset($_SERVER['HTTP_REFERER'])){
			$message .= "User came from: ".$_SERVER['HTTP_REFERER']."\r\n";
		}
		$ipAddress 		= 	$_SERVER["REMOTE_ADDR"];
		if($ipAddress!=''){
			$message .= "Track User Data: http://www.ip-adress.com/ip_tracer/".$ipAddress."\r\n";
		}
		$browser 		= 	$_SERVER['HTTP_USER_AGENT'];
		if($browser!=''){
			$message .= "Browser: ".$browser."\r\n";
		}
		$message.="Page URL they tried to access was: ".home_url().$_SERVER['REQUEST_URI']."\r\n";
		$admin_email 	= 	get_option('admin_email');
		$subject		=	'404 Error at '. home_url();
		//echo $subject;	echo $message; die;
		@wp_mail($admin_email,$subject,$message);
	}
	
	function mail_me_errors(){
		global $wp_query;
		if ($wp_query->is_404){
			email_admin();
		}
	}
	add_action('get_header', 'mail_me_errors');
}

Stay in touch

For a daily email update, click HERE or enter your email and stay on top of things by receiving weekly news in your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>