Monday, December 5, 2022

Get Email Notification When Someone Visits A Particular Page In WordPress

WordPress is one of the most used content management systems. Most of the websites on the internet are built with WordPress and WooCommerce. As per the report published by the W3Techs, 43% of all the websites on the internet are using WordPress. According to a recent trend report by BuiltWith, 29% of the top 1 million websites are developed in WordPress. Many users prefer WordPress websites or landing pages in WordPress for their Marketing campaigns because it is easy to manage content and track performance.

One of my clients recently contacted me for an interesting task of tweaking the default function in WordPress. He actually created one special landing page to advertise his services and wanted to know when someone visits that page for various purposes including sending a discount alert and watching the user’s journey. He wanted me to set up email alerts when someone visits that particular page. So, in this article, I am writing to explain how you can get an email notification when someone visits a particular page in WordPress.

Remember, there could be various ways of achieving this task, you can also get this visit information through Google Analytics or any other such analytics program, or any WordPress plugin. However, this method is very easy and totally specifically for those who want to get a notification from the WordPress website in the form of an email, upon a particular page visited by someone.

How to do it?

This is going to be a very simple and quick process, you just need a simple code and add it to the theme’s function.php file. Editing the function.php for your theme is not recommended though and it is advised to use the function.php file in a child theme.

So to keep it very simple and quick, I would use a WordPress plugin named Code Snippets, which includes my code in the current theme’s function.php file. This way the code will always be connected to the site even when we change the theme or update it.

Steps to get Email notification when someone visits a particular page in WordPress

  1. Install the Code Snippets plugin by Code Snippets Pro.

    Code Snippet plugin
    Code Snippet plugin

    Get Email Notification When Someone...
    Get Email Notification When Someone Visits A Particular Page In WordPress
  2. After the plugin is installed, Go to the option called Snippets on the left pane of the WordPress dashboard and select Add New to add the snippet.

    Add new snippet
    Add new snippet

  3. On the next page give this snippet a name and paste the following code to make it happen. In the if($postid = 261) statement, replace the value 261 with the real post id of the page you want email notification for. Also, replace the other content highlighted in red such as email.
    Paste the code
    Copy and paste code
    $postid = get_the_ID();
    if($postid = 261)
    {
    $to ="contact@hawktest.com";
    $subject = "Page Visit Notification of page marketing Campaign 24 Oct";
    $msg = 'Dear Admin, ';
    $msg .= 'A user just visited the page "marketing Campaign 24 Oct" ';
    $msg .= 'Thank you! ';
    
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: WordPress Alert <wordpress@hawktest.com>' . "\r\n";
    //$headers .= 'CC: info@example.com' . "\r\n";
    wp_mail($to, $subject, $msg, $headers);
    }
    
  4. Finally, click the Save and Activate button at the bottom.
  5. Remember to replace the post id for the page you are tracking or want the email notification for. To get the post ID of the page simply go to the pages menu and hover the mouse on the edit button for the page. Now look for the edit URL at the bottom of the browser, the edit URL has the post=261, similarly, for you, it will be a unique post ID of the page. This id can also be found in the URL when you are editing that particular page.

Get the post ID

I hope you found this interesting and that it helped you achieve your goal. In case you have any questions post your comments below.

 

Categories:

Popular on Hawkdive

- Advertisement -

Latest From Hawkdive

You May like these Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.