Remove Time from Comments in Genesis

I’m working on a site update, so I’m sorting through code and trying to determine what to keep and what needs to be tidied up or removed. One snippet I’ve been using removes the time from comments posted on the site, and leaves just the date.

This code hasn’t given me any issues that I know of, but I was surprised to discover that it was throwing a PHP notice related to the genesis_markup function’s output. I decided to do a bit of digging and discovered that I could greatly simplify the code I was using, thanks to a filter added in Genesis 2.6.0.

The genesis_markup function may be one of the most widely used in Genesis (I did a quick search and came up with nearly 120 implementations of it within the framework alone). I’m still learning how to make use of it. Within the function are multiple dynamic filters, which means that nearly everything going through the markup function can be modified fairly easily.

Dynamic filters are fantastic, because they can be applied with great precision, but when you’re looking in the code for how a filter is applied, it can be harder to find what you’re looking for.

In this instance, I found that I could tinker with just the content (or the actual text inside of the markup) of the comment’s date and time output using this filter within the genesis_markup function:

// Note: This code is copied directly from the Genesis Framework, markup.php file, lines 131-141.

/**
 * Contextual filter to modify 'content'.
 *
 * @since 2.6.0
 *
 * @param string $content Content being passed through Markup API.
 * @param array  $args  Array with markup arguments.
 *
 * @see genesis_markup $args Array.
 */
$content = apply_filters( "genesis_markup_{$args['context']}_content", $args['content'], $args );Code language: PHP (php)

I just need to know what the “context” is: in this case, it’s comment-time-link. I’ll use that to target the dynamic filter hook. All my code needs to do is to return just the comment date, not the time, so I end up with a fairly simple function which looks like this:

add_filter( 'genesis_markup_comment-time-link_content', 'leaven_remove_comment_time' );
/**
 * Remove time from comments.
 * @return string
 */
function leaven_remove_comment_time() {
	return get_comment_date();
}
Code language: PHP (php)

This is a lot cleaner than the code I had before, which required me to rewrite all of the comment date/time output, including the markup.

If you don’t want to show the comment date or time at all, you can remove them completely with this one line:

// Completely remove the date and time from comments.
add_filter( 'genesis_show_comment_date', '__return_false' );Code language: JavaScript (javascript)

Filters are amazing devices in general. It’s even more fantastic when they’re so simple to implement, like this one.

Photo by Rodion Kutsaev

Reader Interactions

Leave a Reply

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

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