How Hide My Dates in WordPress Hides Dates from Search Engines

How Hide My Dates in WordPress Hides Dates from Search Engines

Hello! In this article I will describe to you the internal structure and the principle of operation of the date hiding plugin "Hide My Dates".

The plugin itself is divided into two parts - front end and back end.

The task of the "front end" part (what the site guest sees) is the function that allows you to hide dates.

The "back end" (admin panel) includes more information and a few settings. Which are basically not needed.

Plugin Installation

Open the file “/wp-content/plugins/hide-my-dates/hide-my-dates.php” — it is responsible for how the plugin works. Throughout this article, we will work only with this file.

During plugin installation, initial data is initialized. The following code handles it:

function hmd_init() {
    $opt_date = '1';
	$opt_modifieddate = '1';
	$opt_comments = '1';
    add_option('hmd_opt_date', $opt_date);
	update_option('hmd_opt_date', $opt_date);
	add_option('hmd_opt_modifieddate', $opt_modifieddate);
	update_option('hmd_opt_modifieddate', $opt_modifieddate);
	add_option('hmd_opt_comments', $opt_comments);
	update_option('hmd_opt_comments', $opt_comments);
}
add_action('activate_hide-my-dates/hide-my-dates.php', 'hmd_init');

Honestly, I’m not sure why the data is updated using update_option right after adding it with add_option. Maybe the author just wanted to be safe, or maybe I’m not yet aware of some WordPress internals.

The plugin is multilingual. The following code connects the language file:

function hmd_setup(){
    load_plugin_textdomain('hide-my-dates', null, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
add_action('init', 'hmd_setup');

Back End – Admin Functionality

Since we’re interested in the admin panel, let’s find the function responsible for rendering the plugin’s settings menu item in the WP admin. That would be this code:

function hdm_menu() {
	add_options_page('Hide My Dates', 'Hide My Dates', 'manage_options', 'hide-my-dates.php', 'hdm_options_page');
}

add_action('admin_menu', 'hdm_menu');

Function parameters explained:

  • Hide My Dates (first) — the page title displayed when visiting the plugin settings;
  • Hide My Dates (second) — the menu item label under the “Settings” menu;
  • manage_options — required access capability to use the plugin;
  • hide-my-dates.php — the menu slug; useful for submenus;
  • hdm_options_page — the callback function that renders the plugin settings page.

What we’re interested in is the last argument: hdm_options_page, which forms the settings page. Its contents can be found earlier. I won’t repeat the full function — only important parts:

/* Responsible for receiving settings from the database */
$opt_date = get_option('hmd_opt_date');
$opt_modifieddate = get_option('hmd_opt_modifieddate');
$opt_comments = get_option('hmd_opt_comments');

/* …...... */

/* If POST data was received (i.e. form data was sent to the server), then the condition below will be triggered and the corresponding warning will be displayed */
<?php if ( !empty($_POST ) ) : ?>
<div id="message" class="updated fade"><p><strong><?php _e('Options saved.', "hide-my-dates") ?></strong></p></div>
<?php endif; ?>
<div class="wrap">

/* …...... */

/* The actual settings */
            <tr>
                <th><?php _e("Hide date", "hide-my-dates") ?></th><td><input type="checkbox" name="opt_date" value="1" <?php if ($opt_date == '1') echo "checked='checked'"; ?> /> <?php _e("Post creation date (the_date or the_time) will be hidden from Google.", "hide-my-dates"); ?></td>
            </tr>
			<tr>
                <th><?php _e("Hide modified date", "hide-my-dates") ?></th><td><input type="checkbox" name="opt_modifieddate" value="1" <?php if ($opt_modifieddate == '1') echo "checked='checked'"; ?> /> <?php _e("The last modification date (the_modified_date or the_modified_time) will be hidden from Google.", "hide-my-dates"); ?></td>
            </tr>
			<tr>
                <th><?php _e("Hide dates of comments", "hide-my-dates") ?></th><td><input type="checkbox" name="opt_comments" value="1" <?php if ($opt_comments == '1') echo "checked='checked'"; ?> /> <?php _e("Comment creation dates will be hidden from Google.", "hide-my-dates") ?></td>
            </tr>

To be honest, I spent a long time trying to figure out where the settings were saved. In examples I studied, register_setting was used. I was looking for it, but it turned out to be simpler (note to self). Here’s the code that handles it:

$pluginpage = $_SERVER["REQUEST_URI"];
if(strpos($pluginpage, 'hide-my-dates.php') == true){
if ( isset($_POST['submit']) ) {
	if (!isset($_POST['opt_date'])) $opt_date = '0'; else $opt_date = '1';
    if (!isset($_POST['opt_modifieddate'])) $opt_modifieddate = '0'; else $opt_modifieddate = '1';
	if (!isset($_POST['opt_comments'])) $opt_comments = '0'; else $opt_comments = '1';
    update_option('hmd_opt_date', $opt_date);
	update_option('hmd_opt_modifieddate', $opt_modifieddate);
	update_option('hmd_opt_comments', $opt_comments);
}}

Here we check which file is currently running, and if it’s “hide-my-dates.php” (the plugin page), then we allow updating the data. The check with $_POST['submit'] ensures the form was submitted. Code:

if(strpos($pluginpage, 'hide-my-dates.php') == true){
if ( isset($_POST['submit']) ) {

This concludes the admin section.

Front End – Guest Functionality

One thing I don’t like about this plugin is the stylesheet. I don’t understand why it’s there — it’s just an extra request to the server. The following code connects the CSS file:

function hmd_stylesheet() {
	$purl = plugins_url();
    $myStyleUrl = $purl . '/hide-my-dates/hide-my-dates.css';
    wp_register_style('hide-my-dates', $myStyleUrl);
    wp_enqueue_style( 'hide-my-dates');
}
add_action('wp_print_styles', 'hmd_stylesheet');

At the end of the file, there is a list of add_filter hooks responsible for processing dates in the places we need:

add_filter('get_the_time', 'hide_date');
add_filter('get_the_date', 'hide_date');

add_filter('get_the_modified_time', 'hide_modifieddate');
add_filter('get_the_modified_date', 'hide_modifieddate');

add_filter('get_comment_date', 'hide_comments');
add_filter('get_comment_time', 'hide_comments');

The actual date-hiding logic looks like this:

function hide_date($tdate = '') {
if ( !is_admin() ) {
	$opt_date = get_option('hmd_opt_date');
	if ($opt_date == &quot;1&quot;) {
		$tdate = '&lt;span class=&quot;sdata&quot; title=&quot;' .  $tdate . '&quot;&gt;&lt;/span&gt;';
	}
	}
return $tdate;
}

The logic is simple: if the user is not an admin, retrieve the setting from the database. If date hiding is enabled — wrap the date in a SPAN. Otherwise, show it as-is (text/numbers).

The rest of the hooks work the same way, just referencing other settings and using different CSS classes.

And now for the most interesting part — how dates are actually hidden. Honestly, I thought it used jQuery, but it’s simpler and faster. It’s done via CSS:

.sdata:before{content:attr(title);}

Using the :before pseudo-element, we inject the value of the title attribute into the SPAN.

The content property lets us insert arbitrary text into the HTML, and attr() pulls it from any attribute.

So that’s it — using CSS we reference the needed attribute and output it using a pseudo-element. Simple and effective!

That’s all. Have a great day!

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

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