Skip to content

Instantly share code, notes, and snippets.

@w33zy
Last active October 10, 2019 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w33zy/55fedea70ef392cb6baf359f3719b628 to your computer and use it in GitHub Desktop.
Save w33zy/55fedea70ef392cb6baf359f3719b628 to your computer and use it in GitHub Desktop.
<?php
function generate_json_ld() {
if ( ! is_single() ) {
return null;
}
$data = [];
$data["@context"] = "https://schema.org";
$data["@type"] = "Article";
$data["headline"] = get_the_title();
$data["description"] = get_the_excerpt();
$data["articleBody"] = wp_strip_all_tags( apply_filters( 'the_content', get_the_content() ) );
$data["datePublished"] = get_the_date( 'c' );
$data["dateModified"] = get_the_modified_date( 'c' );
$data["publisher"] = array(
"@type" => "Organization",
"name" => get_bloginfo( 'name' ),
"logo" => array(
"@type" => "ImageObject",
"url" => "https://name.com/wp-content/uploads/2018/01/SmallLogo.jpg",
),
);
$data["mainEntityOfPage"] = array(
"@type" => "WebPage",
"@id" => get_bloginfo( 'url' )
);
$data["author"] = array(
"@type" => "Person",
"name" => get_the_author_meta( 'display_name' ),
"url" => get_the_author_meta( 'user_url' ),
);
$data["image"] = array(
"@type" => "ImageObject",
"url" => get_the_post_thumbnail_url(),
"width" => 1400,
"height" => 425
);
echo '<script class="my-json-markup" type="application/ld+json">' . wp_json_encode( $data ) . '</script>';
}
add_action( 'wp_head', 'generate_json_ld' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment