<?php require_once dirname(__DIR__) . '/vendor/autoload.php'; use LengthOfRope\JSONLD; use LengthOfRope\JSONLD\Schema; $Create = JSONLD\Create::factory()->add(Schema\BookSchema::factory()->setAuthor(Schema\PersonSchema::factory()->setName("Bas de Kort")->setEmail("*****@*****.**"))->setAbout("PHP")->setName("Superb PHP Book")->setAlternateName("Book one of three"))->add(Schema\OrganizationSchema::factory()->setAddress(Schema\PostalAddressSchema::factory()->setPostalCode("1234 AA")->setStreetAddress("Somewhere 12")->setAddressCountry("NL")->setAddressLocality("Amersfoort")->setEmail("*****@*****.**")->setTelephone("033-1234567")->setAddressRegion("Utrecht"))->setName("LengthOfRope")->setDescription("Just another developer")); //echo "<pre>"; //print_r($Create->getDataArray()); print_r($Create->getJSONLDScript());
/** * Output JSON-LD data in the loop of the HTML page if required */ public function addJSONLDToLoop($content) { // Not a single page, bail early if (!is_singular()) { return $content; } // Get organization $Org = $this->getOrganization(); // Check if we don't want the boxes to show in code, we also don't want to output jsonld $pt = get_post_type(); if (in_array($pt, apply_filters('buzz-seo-disable-posttype', array()))) { return $content; } $options = get_option('_settingsSettingsStructuredData', true); $postAuthorID = get_post_field('post_author', get_queried_object_id()); $Create = \LengthOfRope\JSONLD\Create::factory(); $hasData = false; // Add Article if (isset($options['addarticle']) && is_array($options['addarticle'])) { $posttype = get_post_type(); foreach ($options['addarticle'] as $creativeWorkType => $postTypes) { if (!is_array($postTypes)) { continue; } $addForPostTypes = array_keys($postTypes); if (in_array($posttype, $addForPostTypes)) { // We are in a posttype that we want to add the article data for, but since we are not in the loop, get // the author data in an arbitrary way $class = "\\LengthOfRope\\JSONLD\\Schema\\" . $creativeWorkType . "Schema"; $Schema = $class::factory(); if ($Schema instanceof Schema\ThingSchema) { $Schema->setName(get_the_title()); $url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full'); if (is_array($url)) { $Schema->setImage(Schema\ImageObjectSchema::factory()->setUrl($url[0])->setWidth($url[1])->setHeight($url[2])); } else { // Skip this post since it has no image continue; } if (has_excerpt()) { $excerpt = get_the_excerpt(); if (!empty($excerpt)) { $Schema->setDescription(strip_tags($excerpt)); } } } // Article and blog require a headline, we simply set it to the post title. if ($Schema instanceof Schema\ArticleSchema || $Schema instanceof Schema\BlogPostingSchema) { $Schema->setHeadline(get_the_title()); } if ($Schema instanceof Schema\CreativeWorkSchema) { $Schema->setDatePublished(get_the_date('c')); $Schema->setDateModified(get_the_modified_date('c')); // Publisher is required if ($Org !== false) { // Set the organization if availble $Schema->setPublisher($Org); } // mainEntityOfPage is recommended $Schema->setMainEntityOfPage(get_permalink()); } // Add author $Author = false; if (isset($options['addauthor']) && is_array($options['addauthor']) && isset($options['addauthor'][$creativeWorkType])) { $authorF = get_the_author_meta('first_name', $postAuthorID); $authorL = get_the_author_meta('last_name', $postAuthorID); $author = trim($authorF . " " . $authorL); $authormail = get_the_author_meta('email', $postAuthorID); $authorurl = get_the_author_meta('url', $postAuthorID); $Author = Schema\PersonSchema::factory(); if (!empty($author)) { $Author->setName($author); } if (!empty($authorF)) { $Author->setGivenName($authorF); } if (!empty($authorL)) { $Author->setFamilyName($authorL); } if (!empty($authormail)) { $Author->setEmail($authormail); } if (!empty($authorurl)) { $Author->setUrl($authorurl); } $Schema->setAuthor($Author); } if ($Org === false && $Author !== false) { // Set the author as publisher if organization is not available $Schema->setPublisher($Author); } $hasData = true; $Create->add($Schema); } } } if ($hasData) { return $content . $Create->getJSONLDScript() . PHP_EOL; } return $content; }