Created
January 22, 2020 12:39
-
-
Save jdevalk/cb0be1d345d58642fbe2853361fecb3f to your computer and use it in GitHub Desktop.
This is the code used to generate the Event Schema for YoastCon
This file contains 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 | |
/** | |
* Adds Schema pieces to our output. | |
* | |
* @param array $pieces Graph pieces to output. | |
* @param \WPSEO_Schema_Context $context Object with context variables. | |
* | |
* @return array $pieces Graph pieces to output. | |
*/ | |
function add_graph_pieces( $pieces, $context ): array { | |
$pieces[] = new YoastCon( $context ); | |
return $pieces; | |
} | |
add_filter( 'wpseo_schema_graph_pieces', 'yoast_add_graph_pieces', 11, 2 ); |
This file contains 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 | |
use WPSEO_Schema_Context; | |
/** | |
* Class Event | |
* | |
* @package Yoast\YoastCom\Core\WordPress\Integration\YoastSEO | |
*/ | |
class YoastCon implements \WPSEO_Graph_Piece { | |
/** | |
* A value object with context variables. | |
* | |
* @var \WPSEO_Schema_Context | |
*/ | |
private $context; | |
/** | |
* The Event data. | |
* | |
* @var array | |
*/ | |
private $data; | |
/** | |
* Event constructor. | |
* | |
* @param WPSEO_Schema_Context $context | |
*/ | |
public function __construct( \WPSEO_Schema_Context $context ) { | |
$this->context = $context; | |
} | |
/** | |
* Determine whether we should output an Event piece. | |
* | |
* @return bool | |
*/ | |
public function is_needed(): bool { | |
if ( ! is_page( 1376069 ) ) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Generates our Event data. | |
* | |
* @return array | |
*/ | |
public function generate() { | |
$this->data = [ | |
'@type' => 'BusinessEvent', | |
'@id' => $this->context->canonical . '#yoastcon', | |
'mainEntityOfPage' => [ '@id' => $this->context->canonical . \WPSEO_Schema_IDs::WEBPAGE_HASH ], | |
'name' => get_post( $this->context->id )->post_title, | |
'description' => $this->context->description, | |
'organizer' => [ '@id' => $this->context->site_url . \WPSEO_Schema_IDs::ORGANIZATION_HASH ], | |
'startDate' => date( 'c', strtotime( '24 April 2020 08:00 CET' ) ), | |
'endDate' => date( 'c', strtotime( '24 April 2020 17:00 CET' ) ), | |
'url' => $this->context->canonical, | |
'image' => 'https://yoast.com/app/uploads/2019/10/YoastCon2020-600x338.png', | |
'location' => [ | |
'@type' => 'Place', | |
'name' => 'Theater \'t Mozaïek', | |
'url' => 'https://www.mozaiekwijchen.nl/', | |
'address' => [ | |
'@type' => 'PostalAddress', | |
'addressLocality' => 'Wijchen', | |
'addressRegion' => 'Gelderland', | |
'addressCountry' => 'The Netherlands', | |
'postalCode' => '6602 HX', | |
'streetAddress' => 'Campuslaan 6', | |
], | |
], | |
]; | |
return $this->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example code. Works perfect.