Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/**
* @package Structured Data of JSON-LD
* @version 2.0
*/
/*
Plugin Name: Structured Data of JSON-LD
Plugin URI: http://wordpress.org/plugins/ejls-easy-json-ld-setter/
Description: Set Structured Data of "JSON-LD" to your WebSite.schema type that you can use is "Article","Person","WebSite" and "searchAction".
Author: Hidetaka Okamoto
Version: 2.0
Author URI: http://wp-kyoto.net/
*/
add_action( 'wp_head', 'ejls_insert_json_ld' );
function ejls_get_article () {
if ( is_page() || is_single() ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
$contentArr['@type'] = 'Article';
$contentArr['name'] = get_the_title();
// @type:Article は headline でっていうから。。
$contentArr['headline'] = get_the_title();
// datePublished は ISO_8601 でっていうから。。
$time = strtotime( get_the_time('c') );
$contentArr['datePublished'] = date( 'c', $time );
// get_post_thumbnail_id がなかった場合、記事の最初の画像を入れる。それさえもなかったら。。。
$contentArr['image'] = ejls_post_thumbnail();
// $contentArr['image'] = wp_get_attachment_url( get_post_thumbnail_id() );
$contentArr['url'] = get_permalink();
$contentArr['articleBody'] = get_the_content();
$contentArr['author']['@type'] = 'Person';
$contentArr['author']['name'] = get_the_author();
$contentArr['publisher']['@type'] = 'Organization';
$contentArr['publisher']['name'] = get_bloginfo('name');
endwhile; endif;
rewind_posts();
return $contentArr;
}
}
function ejls_get_search_Action( $homeUrl ){
$contentArr = array(
"@type" => "SearchAction",
"target" => "{$homeUrl}/?s={search_term}",
"query-input"=> "required name=search_term"
);
return $contentArr;
}
function ejls_insert_json_ld(){
$homeUrl = get_home_url();
$contentArr = array(
"@context" => "http://schema.org",
);
if (is_front_page()) {
$contentArr['@type'] = "WebSite";
$contentArr['url'] = $homeUrl;
$contentArr['potentialAction'] = ejls_get_search_Action($homeUrl);
} elseif (is_page() || is_single()) {
$contentArr['@graph'] = ejls_get_article();
}
$jsonld = json_encode($contentArr, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
echo '<script type="application/ld+json">';
echo $jsonld;
echo '</script>';
}
// 記事中の最初の画像があるかないか。
function ejls_catch_that_image() {
global $post;
if ( preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches ) ) {
$ejls_first_img = $matches[1][0];
} else {
$ejls_first_img = false;
}
return $ejls_first_img;
}
// アイキャッチに画像があればアイキャッチを。なければ記事中にある最初の画像を。それさえもなければ適当に画像いれます??
function ejls_post_thumbnail() {
if ( get_post_thumbnail_id() ) {
$ejls_img = wp_get_attachment_url( get_post_thumbnail_id() );
} elseif( ejls_catch_that_image() ) {
$ejls_img = ejls_catch_that_image();
} else {
$ejls_img = "適当にOGPで設定されてるURLでもGoogleさんのテスターでOkでた。謎";
}
return $ejls_img;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment