Last active
September 14, 2020 11:56
-
-
Save AustinGil/d3be2b1df8b7439066abcc21a2bf488e to your computer and use it in GitHub Desktop.
JSON-LD for WordPress posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Remove hentry from post_class | |
* This is important to not get schema errors | |
*/ | |
function visceral_remove_hentry_class( $classes ) { | |
$classes = array_diff( $classes, array( 'hentry' ) ); | |
return $classes; | |
} | |
add_filter( 'post_class', 'visceral_remove_hentry_class' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adding JSON-LD for structured data and SEO | |
* | |
* Official website: http://json-ld.org/ | |
* Examples: http://jsonld.com/ | |
* Generators: https://www.google.com/webmasters/markup-helper/u/0/ | |
* https://www.schemaapp.com/tools/jsonld-schema-generator/ | |
* Test: https://search.google.com/structured-data/testing-tool/u/0/ | |
* Google Docs: https://developers.google.com/search/docs/data-types/data-type-selector | |
**/ | |
$post_id = get_the_id(); // Store the ID | |
// JSON-LD amrkup For Articles | |
if ( 'post' == get_post_type() ) : | |
$logo_url = get_site_icon_url(); // Feel free to customize. Otherwise will use WordPress site icon | |
if ( $logo_url && has_post_thumbnail() ) : | |
// Images are required. We only continue if we have them. | |
$featured_img = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full', true ); | |
// JSON-LD markup for Articles | |
$json_ld = array( | |
// Required | |
"@context" => "http://schema.org", | |
"@type" => "Article", | |
"publisher" => array( | |
"@type" => "Organization", | |
"url" => get_site_url(), | |
"name" => get_bloginfo('name'), | |
"logo" => $logo_url | |
), | |
"headline" => get_the_title(), | |
"author" => array( | |
"@type" => "Person", | |
"name" => get_the_author() | |
), | |
"datePublished" => get_the_date(), | |
"image" => array( | |
"@type" => "ImageObject", | |
"url" => $featured_img[0], | |
"width" => $featured_img[1], | |
"height" => $featured_img[2] | |
), | |
// Recommended | |
"dateModified" => get_the_modified_date(), | |
"mainEntityOfPage" => get_permalink(), | |
// Optional (Not sure if they affect SEO) | |
// "url" => get_permalink(), | |
// "description" => get_the_excerpt(), | |
// "articleBody" => get_the_content(), | |
); | |
?> | |
<script type='application/ld+json'> | |
<?php echo json_encode( $json_ld ); ?> | |
</script> | |
<?php endif; | |
endif; | |
// JSON-LD markup for Persons | |
if ( 'person' == get_post_type() ) : | |
$job_title = get_post_meta( $post_id, 'job_title', true); // Edit meta field name as needed | |
$email = get_post_meta( $post_id, 'email', true); // Edit meta field name as needed | |
$json_ld = array( | |
"@context" => "http://schema.org", | |
"@type" => "Person", | |
"name" => get_the_title() | |
); | |
if ( has_post_thumbnail() ) { | |
$featured_img = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full', true ); | |
$json_ld['image'] = array( | |
"@type" => "ImageObject", | |
"url" => $featured_img[0], | |
"width" => $featured_img[1], | |
"height" => $featured_img[2] | |
); | |
} | |
// These variables come from post meta above | |
if ( $title ) { | |
$json_ld['jobTitle'] = $title; | |
} | |
if ( $email ) { | |
$json_ld['email'] = $email; | |
} | |
?> | |
<script type='application/ld+json'> | |
<?php echo json_encode( $json_ld ); ?> | |
</script> | |
<?php endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment