Converting WordPress Themes for Microformats 2 – Part 1

I won’t claim to be a Microformats expert…but the below are some simple steps that can be taken to adjust a theme structurally for Microformat posts.

The below filters can be added to a theme’s functions.php, but you have to make sure that your theme uses the post, body, and comment class functions, and that it doesn’t style hfeed or hentry. Also, hfeed is often added to the theme, and should be removed to avoid duplication.

 

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
		$classes[] = 'h-feed';
	} else {
		if ( 'page' !== get_post_type() ) {
				$classes[] = 'hentry';
				$classes[] = 'h-entry';
		}
	}
	return $classes;
}
add_filter( 'body_class', 'body_classes' );
/**
 * Adds custom classes to the array of post classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function post_classes( $classes ) {
	$classes = array_diff( $classes, array( 'hentry' ) );
	if ( ! is_singular() ) {
		if ( 'page' !== get_post_type() ) {
			// Adds a class for microformats v2
			$classes[] = 'h-entry';
			// add hentry to the same tag as h-entry
			$classes[] = 'hentry';
		}
	}
	return $classes;
}

add_filter( 'post_class', 'post_classes' );

 

Now the below adds microformats 2 classes to the avatar photo and to comments.

/**
 * Adds mf2 to avatar
 *
 * @param array             $args Arguments passed to get_avatar_data(), after processing.
 * @param int|string|object $id_or_email A user ID, email address, or comment object
 * @return array $args
 */
	function get_avatar_data($args, $id_or_email) {
	if ( ! isset( $args['class'] ) ) {
		$args['class'] = array( 'u-photo' );
	 } else {
		$args['class'][] = 'u-photo';
	 }
	return $args;
	}

add_filter( 'get_avatar_data', 'get_avatar_data', 11, 2 );

/**
 * Adds custom classes to the array of comment classes.
 */
function comment_classes( $classes ) {
	$classes[] = 'u-comment';
	$classes[] = 'h-cite';
	return array_unique( $classes );
}

add_filter( 'comment_class', 'comment_classes', 11 );

This allows for the simplest conversion of themes to the basic Microformats 2 structure. In the second part, we start moving into other more invasive modifications of the theme.

David Shanske

My day job is in training for an airline. I also develop Indieweb WordPress plugins so that others can take control of their online identity.

14 Responses

  1. For those interested in more and who know a bit of code, David’s also got a “master class” on adding microformats to modern themes in his commit trail for updating/upgrading the TwentySixteen theme to be more IndieWeb friendly: https://github.com/dshanske/twentysixteen-indieweb/commits/master

    While it isn’t comprehensive and may not cover every eventuality for every theme, following along with his commits here will get you a long way towards better understanding microformats v2 use with WordPress. I think I’ve learned more about WordPress themes and microformats by following his changes here than anything else I’ve tried.

    1. Sad, that TwentySixteen is the only microformat2 WordPress theme I could find. And TwentySixteen is ugly. I may be going back to less microformats friends but more attractive them suitable for a website that has a lot of photographic posts.

Leave a Reply

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