Skip to content

Instantly share code, notes, and snippets.

@pixelstorm
Last active August 24, 2024 19:41
Show Gist options
  • Save pixelstorm/f49ae19ebf02ca92beced7f10d7295b1 to your computer and use it in GitHub Desktop.
Save pixelstorm/f49ae19ebf02ca92beced7f10d7295b1 to your computer and use it in GitHub Desktop.
schema_cheatsheet
add_filter( 'wpseo_schema_graph_pieces', 'yoast_add_graph_pieces', 11, 2 );
//sample adding the person content type
/**
* 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 yoast_add_graph_pieces( $pieces, $context ) {
$pieces[] = new Team_Member( $context );
return $pieces;
}
//add the team member by extending the person class
/**
* Class Team_Member
*/
class Team_Member extends \WPSEO_Schema_Person implements \WPSEO_Graph_Piece {
/**
* A value object with context variables.
*
* @var WPSEO_Schema_Context
*/
private $context;
/**
* Team_Member constructor.
*
* @param WPSEO_Schema_Context $context Value object with context variables.
*/
public function __construct( WPSEO_Schema_Context $context ) {
$this->context = $context;
}
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
if ( is_singular( 'yoast_team_member' ) ) {
return true;
}
return false;
}
/**
* Adds our Team Member's Person piece of the graph.
*
* @return array $graph Person Schema markup
*/
public function generate() {
$data = parent::generate();
$data['worksFor'] = [ '@id' => $this->context->site_url . \WPSEO_Schema_IDs::ORGANIZATION_HASH ];
$data['mainEntityOfPage'] = [ '@id' => $this->context->canonical . \WPSEO_Schema_IDs::WEBPAGE_HASH ];
$job_title = get_post_meta( $this->context->id, 'employees_function_title', true );
if ( ! empty( $job_title ) ) {
$data['jobTitle'] = $job_title;
}
$suffix = get_post_meta( $this->context->id, 'employees_honoric_suffix', true );
if ( ! empty( $suffix ) ) {
$data['honorificSuffix'] = $suffix;
}
return $data;
}
}
@pixelstorm
Copy link
Author

https://developer.yoast.com/schema-documentation/integration-guidelines/

implementeing custom json ld schema into a yoast install.
Yoast outputs some basic markup for json ld. most of the time this is ok but if you need more.
then you need to output custom "content type" peices and stich it to the main graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment