Skip to content

Instantly share code, notes, and snippets.

@NormanHoehne
Forked from nathanrice/functions.php
Created March 3, 2016 10:59
Show Gist options
  • Save NormanHoehne/256994c9fc8659eeb940 to your computer and use it in GitHub Desktop.
Save NormanHoehne/256994c9fc8659eeb940 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>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment