Skip to content

Instantly share code, notes, and snippets.

@nathanrice
Created January 6, 2016 16:41
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanrice/b90388e6b8052bf60583 to your computer and use it in GitHub Desktop.
Save nathanrice/b90388e6b8052bf60583 to your computer and use it in GitHub Desktop.
Add back blogPosting Schema to Genesis sites
<?php
/*
Add back Schema.org/blogPosting microdata to post entries.
Replace all instances of "themedemo" with something unique to your site.
User input is required in the last function. Be sure to fill these fields in with your own information.
Instances where you need to fill in information will be marked with a comment that indicates so.
*/
add_filter( 'genesis_attr_body', 'themedemo_attributes_body' );
/**
* If viewing blog post or archive, make this a Blog page type.
*/
function themedemo_attributes_body( $attributes ) {
if ( is_singular( 'post' ) || is_archive() || is_home() || is_page_template( 'page_blog.php' ) ) {
$attributes['itemtype'] = 'http://schema.org/Blog';
}
return $attributes;
}
add_filter( 'genesis_attr_entry', 'themedemo_attributes_entry' );
/**
* Add Schema.org microdata to the entry.
*
* No need to change any of this.
*/
function themedemo_attributes_entry( $attributes ) {
//* Only target main query entries
if ( ! is_main_query() && ! genesis_is_blog_template() ) {
return $attributes;
}
if ( 'post' === get_post_type() ) {
$attributes['itemscope'] = true;
$attributes['itemtype'] = 'http://schema.org/BlogPosting';
//* If not search results page
if ( ! is_search() ) {
$attributes['itemprop'] = 'blogPost';
}
}
return $attributes;
}
add_action( 'genesis_entry_content', 'themedemo_extra_entry_meta', 5 );
/**
* Extra recommended meta data about the entry
*/
function themedemo_extra_entry_meta() {
printf( '<meta itemprop="dateModified" content="%s" />', esc_attr( get_the_modified_time( 'c' ) ) );
printf( '<meta itemprop="mainEntityOfPage" content="%s" />', esc_url( get_permalink() ) );
}
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
add_action( 'genesis_entry_content', 'themedemo_entry_image', 8 );
/**
* Output the entry image object.
*/
function themedemo_entry_image() {
$img = genesis_get_image( array(
'format' => 'html',
'size' => genesis_get_option( 'image_size' ),
'context' => is_singular() ? 'singular' : 'archive',
'attr' => genesis_parse_attr( 'entry-image', array ( 'alt' => get_the_title() ) ),
) );
list( $url, $width, $height ) = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
if ( ! empty( $img ) ) {
$img = sprintf( '<a href="%s" aria-hidden="true">%s</a>', get_permalink(), $img );
}
echo '<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
echo $img;
printf( '<meta itemprop="url" content="%s">', esc_url( $url ) );
printf( '<meta itemprop="width" content="%s">', esc_attr( $width ) );
printf( '<meta itemprop="height" content="%s">', esc_attr( $height ) );
echo '</div>';
}
add_action( 'genesis_entry_content', 'themedemo_entry_publisher' );
/**
* Output publisher information.
*
* Requires user input. Replace values below with ones that you choose.
*/
function themedemo_entry_publisher() {
// Is the publisher a person or organization?
// Uncomment your choice below.
//$publisher = 'https://schema.org/Organization';
//$publisher = 'https://schema.org/Person';
$publisher_name = 'ACME Inc.'; // Replace this with the publisher's name
$logo = 'http://example.com/image.jpg'; // Replace this with the URL to your logo
$logo_width = 800; // Replace this with your logo width
$logo_height = 800; // Replace this with your logo height
//* Do nothing if user hasn't uncommented a publisher type above
if ( ! isset( $publisher ) ) {
return;
}
printf( '<div itemprop="publisher" itemscope itemtype="%s">', esc_url( $publisher ) );
echo '<div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">';
printf( '<meta itemprop="url" content="%s">', esc_url( $logo ) );
printf( '<meta itemprop="width" content="%d">', esc_attr( $logo_width ) );
printf( '<meta itemprop="height" content="%d">', esc_attr( $logo_height ) );
echo '</div>';
printf( '<meta itemprop="name" content="%s">', esc_attr( $publisher_name ) );
echo '</div>';
}
@rgoldston
Copy link

I don't know what the inline instruction 'Replace all instances of "themedemo" with something unique to your site.' is supposed to mean. Can someone please elaborate

@consciousimages
Copy link

@rgoldston - it's not necessary, those are the function names, and they can remain that way if you don't want to bother. He's just suggesting that those names should probably be something more specific to your site, or something that means more to you (maybe a reminder that those functions came from this page, or something you didn't write). It's a suggestion rather than a requirement.

@spaceman6
Copy link

spaceman6 commented Dec 13, 2016

[Update: Argh, nevermind. ARIA was just not working in my browser. Fixed.]

Is there any way to put a display:none in the image tag that it outputs after the <a href="%s" aria-hidden="true">%s</a> bit?

The reason being twofold.

  1. Styling the Entry-Image is a little easier if I can make my own CSS-classed div
  2. Sometimes, the entry has both a Video in the excerpt and a still-image that functions as the official Featured Image for the post.

The way it is in the code puts the image in-addition to the ones I already manually-declared at the beginning of both the entry & excerpt.

Any help would be appreciated! -Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment