How to create a WordPress plugin to trackGooglebot visits

Aug 24, 2025 7 minutes to read
views

If you have a WordPress website, you’ve probably used plugins to add new features without coding everything from scratch. But what if you can’t find the exact functionality you need?

That’s when it’s time to write a WordPress plugin yourself. In this guide, we’ll walk you through how to create a WordPress plugin that detects Googlebot visits, records them in the database, and displays the data right in your admin panel.

Why you might need a custom WordPress plugin

While the WordPress plugin ecosystem is huge, there are situations where you need something unique:

  • Specific tracking or automation tasks not covered by existing plugins.
  • Integrations with internal systems or custom APIs.
  • Lightweight alternatives to bloated third-party tools.
  • Functionality that needs to follow your own WordPress plugin development best practices.

Who benefits from this?

Marketers who want better SEO insights, developers who need a tailor-made tool, and site owners looking for full control over their site’s features.

What problem are we solving?

We’re building a plugin that will:

  • Detect when Googlebot visits a page or post.
  • Store the date and time of the last visit in the database.
  • Display the information inside the WordPress dashboard.

If you’ve ever wanted to know exactly when Google last crawled your content, this is the perfect solution.

What you’ll need before we start

To build a WordPress plugin like this, prepare:

  • A working WordPress installation (local or server).
  • Basic knowledge of PHP syntax.
  • Access to edit files inside your wp-content/plugins directory.

WordPress Plugin Structure

Every WordPress plugin is placed inside the wp-content/plugins directory of your WordPress installation. Each plugin is organized in its own subfolder. In this structure, each folder represents a separate plugin.

The overall plugin structure can vary depending on the complexity, architecture, and development practices used. However, WordPress requires a few basic rules that every plugin must follow:

  1. The plugin must be contained within its own folder, e.g., plugin-name/.
  2. That folder must contain at least one file — the main plugin file, e.g., plugin-name.php.

It’s a good practice to name the main plugin file the same as the folder. This helps ensure WordPress can correctly detect and identify your plugin.

Here’s an example of a minimal plugin folder structure:

/plugin-name
 plugin-name.php
 uninstall.php
 /languages
 /includes
 /admin
   /js
   /css
   /images
 /public
   /js
   /css
   /images

Not all of these subfolders are required — each one should be added only if needed.

For example:

  • You don’t need the /languages folder unless you plan to support multiple languages.
  • The /admin and /public folders are typically used for storing assets (JS/CSS/images) for the admin dashboard and the front end of the site, respectively.

Choosing the right plugin architecture: simple vs scalable

The way you structure your plugin depends on its size and purpose. If you’re building something small and single-purpose, there’s no need to overcomplicate it with dozens of files and folders. A clean, well-commented single-file plugin can work perfectly, as long as it’s easy to read and maintain.

For larger or more complex projects, it’s worth applying core WordPress plugin development best practices and software design principles, such as:

  • DRY (Don’t Repeat Yourself) — avoid duplicating code.
  • KISS (Keep It Simple, Stupid) — don’t overengineer.
  • Separation of Concerns — keep different parts of the code independent.

Following these rules makes your plugin easier to manage, scale, and maintain — especially in team environments or when revisiting the code months later.

If you want to go deeper, the official WordPress Plugin Handbook is an excellent resource for both beginners and advanced developers.

For our task, since we’re solving one clear problem, the optimal choice is a single-file architecture with a few well-structured functions — simple, fast, and efficient.

Step-by-step: creating the Googlebot visit tracker plugin

creating-the-googlebot-visit-tracker-plugin

We’ll assume you already have WordPress installed and know how to edit files within your WordPress setup.

First, navigate to the wp-content/plugins folder and create a new directory for your plugin. In this example, we’ll call it googlebot-visit-tracker.

Inside this folder, you’ll add the main plugin file. This file contains all the essential information WordPress needs to recognize your plugin in the Admin dashboard under Plugins.

Let’s create our main file, googlebot-visit-tracker.php, with the following content: 

<?php
/**
 * Plugin Name: Googlebot Visit Tracker
 * Description: Tracks the last time Googlebot visited a post or page.
 * Version: 1.0
 * Author: Deveit
 */

if ( ! defined( 'ABSPATH' ) ) {
          exit; // Exit if accessed directly
}

Congratulations — your plugin is now created! You should see it listed in the Plugins section of your WordPress dashboard, ready to be activated or deactivated with a single click. Go ahead and activate it to start using it.

If you look at the code, you’ll notice it begins with a special comment block containing fields such as Plugin Name, Description, Author, and Version.

WordPress uses this information to recognize and display your plugin in the Admin panel. While only Plugin Name is required, adding the other fields provides helpful context for anyone managing the plugin later.

You’ll also see a security check for the ABSPATH constant. This ensures the plugin file can only be executed within the WordPress environment, preventing direct access from outside. 

How to Detect Googlebot Visits to Your Singular Posts and Pages

how-to-detect-googlebot-visits-to-your-singular-posts-and-pages

A user agent is a piece of software — often a web browser — that acts on behalf of the user to retrieve and interact with online content. Think of it as the “middleman” between a user’s device and a website or application.

When a user agent sends an HTTP request to a server, it includes a User-Agent header that identifies the software and its version.

When Google crawls a website, it can use different user agent strings, for example:

  • Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
  • Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36

In every case, these strings contain the word “Googlebot”, which makes it a reliable way to detect visits from Google’s crawler.

However, we don’t need to detect it on every page. In our plugin, we only want to track Googlebot visits when it’s crawling singular pages (a single post or page).

We can do this by checking if the current page is singular using the is_singular() function.

For example, the following code runs only on singular pages and skips all other types: 

if (is_singular()) {
...
}

How We Can Update Information About the Latest Visit in the Metadata

Now that we can detect when the visitor is Googlebot and confirm it’s accessing a singular page, we can store this data in the post’s metadata.

In WordPress, global variables hold information about the currently loaded objects. For posts and pages, details are stored in the global $post variable. We’ll use it to determine which post should be updated.

To save our data, we’ll use the built-in WordPress function:

update_post_meta($postID, $meta_key, $meta_value)

Now, let’s combine all these parts into a new action added to our main plugin file:

_action('template_redirect', function() {
   if (is_singular()) {
      $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
      if (str_contains($user_agent, 'Googlebot')) {
         global $post;
         if ($post && isset($post->ID)) {
            update_post_meta($post->ID, '_last_googlebot_visit', current_time('mysql'));
         }
      }
   }
});

Great! Now, every time a page loads, our plugin runs this check. If the visitor is Googlebot and it’s accessing a singular post, the plugin updates that post’s metadata with the exact date and time of the visit.

How to Show Our New Information as a Metabox

how-to-show-our-new-information-as-a-metabox

We can now capture and store Googlebot visit data, but it’s only useful if we can view it in the admin panel. To do this, we’ll add a metabox to the post/page edit screen that displays the date and time of the last Googlebot visit.

WordPress provides native functionality for creating metaboxes. First, we need a function that will output the content inside the metabox. For example:

function render_googlebot_visit_meta($post) {
  $last_visit = get_post_meta($post->ID, '_last_googlebot_visit', true);
  echo $last_visit
   ? '<p>Last visited by Googlebot on: <strong>' . esc_html($last_visit) . '</strong></p>'
   : '<p>Googlebot has not visited this page yet.</p>';
}

Once we have a function for rendering data into the metabox, we can register our metabox for post and page edit screens using this simple code:

add_action('add_meta_boxes', function() {
 add_meta_box(
  'googlebot_visit_meta',
  'Last Googlebot Visit',
  'render_googlebot_visit_meta',
  ['post', 'page'],
  'side'
 );
});

As a result, when you open the post or page editor, you will see a new metabox on the right side of the screen displaying the information from our plugin:

Showing Last Googlebot Visit Datetime in the List

showing-last-googlebot-visit-datetime-in-the-list

In addition to displaying the visit date on the edit screen, it’s useful to show it directly in the posts list. This way, you can quickly check when Googlebot last visited each post without opening them one by one.

To do this, first register a new column in the posts table using the manage_post_posts_columns filter. Add the following code to your main plugin file:

add_filter('manage_post_posts_columns', function($columns) {
 $columns['googlebot_visit'] = 'Googlebot Visit';
 return $columns;
});

Then, we need to define the logic for rendering data in this new column. We’ll use the manage_post_posts_custom_column action and define the rules to display data only for the column we just created:

add_action('manage_post_posts_custom_column', function($column, $post_id) {
  if ($column === 'googlebot_visit') {
      $last_visit = get_post_meta($post_id, '_last_googlebot_visit', true);
      echo $last_visit ? esc_html($last_visit) : '—';
  }
}, 10, 2);

Once this code is in place, you can open the Screen Options panel in the WordPress admin and enable the display of the new “Googlebot Visit” column.

That’s it — your plugin now captures Googlebot visits to your pages and displays the date and time both in the post edit screen and directly in the posts list.

Conclusion

In this article, we showed how to create a simple WordPress plugin that detects specific visitors — such as Googlebot — and records the date and time of their last visit in the database. We also explained how to display this data in the WordPress admin, both on the post edit screen and in the posts list.

This is a basic example, but the same approach can be used to develop more advanced solutions with additional tracking, reporting, and integrations tailored to your needs.

Need a custom WordPress plugin for your website?

We create plugins that follow WordPress development best practices, work reliably, and integrate seamlessly into your site. Share your requirements with us, and we’ll build a solution that delivers exactly what you need — on time and with full transparency.

Contact us at sales@deveit.com to start your project.

Rate this article
All Blogs

Contact us

Our expert team is here to help. Submit your details and we will contact you within 24 hours