WordPress | Shortcode to output JSON-LD structured data for Person schema based on CPT + ACF fields
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 | |
/* take some of the fields from the Team Member and use it to make a shortcode that outputs JSON-LD structured data for Person schema */ | |
add_shortcode( 'fs-team-schema', 'fs_team_show_json_schema_shortcode' ); | |
function fs_team_show_json_schema_shortcode() { | |
// if within an admin page and its not doing Ajax request | |
if ( is_admin() AND ! wp_doing_ajax() ) { | |
// then don't return anything since we aren't using it here | |
return FALSE; | |
} | |
// JSON CONTENT CHUNK | |
// initially set it to blank, just in case | |
$json_content = ''; | |
// NOTE: when using json_enconde, it ensures quotes are escaped from within the content/string, and also wraps quotes around the overall content (within the json_encode) for us (which is why we don't wrap them ourselves in the variable) | |
// just be careful on where you add the comma to separate each key/value pair, since there can be cases where there is no key/value pair preceeding one, so we can't have a comma at the end if nothing comes after it | |
// get the post Title for the Person's Name | |
$team_member_name = json_encode(get_the_title(get_the_ID())); | |
// get the post URL for part of the Person's ID, and then add #person at the end of it | |
$team_member_id = json_encode(get_the_permalink(get_the_ID()) . '#person'); | |
// get the post URL for the Person's URL | |
$team_member_url = json_encode(get_the_permalink(get_the_ID())); | |
// get the ACF position field for the Job Title | |
$team_member_position_code = ''; | |
$team_member_position = get_field('position'); | |
if ( $team_member_position ) { | |
$team_member_position_code = '"jobTitle": ' . json_encode($team_member_position) . ','; | |
} | |
// get the Excerpt content for the Description | |
$team_member_description_code = ''; | |
$team_member_description = get_the_excerpt(get_the_ID()); | |
// only if it has a true excerpt, otherwise it will return its own excerpt based on the content, which we don't want | |
if( has_excerpt ( get_the_ID() ) ) { | |
$team_member_description_code = '"description": ' . json_encode($team_member_description) . ','; | |
} | |
// get the Featured Image URL for the Image | |
$team_member_image_code = ''; | |
if ( has_post_thumbnail( get_the_ID() ) ) { | |
$team_member_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); | |
$team_member_image_code = '"image": ' . json_encode($team_member_image[0]) . ','; | |
} | |
// get the Social Links for the Person's Same As links | |
$social_links_code = ''; | |
$team_member_url_comma = ''; | |
// get the Social Links group field | |
$social_links = get_field('social_links'); | |
// check to see if any of the sub fields exist (since, with a group field you need to check for values in each of the fields in the group to see if they exist, since the group itself will always say it exists) | |
if ( $social_links['linkedin'] || $social_links['facebook'] || $social_links['github'] || $social_links['twitter'] ) { | |
// if there are social links, then that means the sameAs will come after the url property in the JSON output, so we need to ensure there is a comma after the url property (and only if we have social links) | |
$team_member_url_comma = ','; | |
$social_links_code = '"sameAs": '; | |
// go through the Social Links array and output each one (but only the values, and not both the key and value), to get any of the subfield values (URLs) | |
// and make them each be comma separated (whilst each is encoded properly, each wrapped in quotes, and surrounded by brackets ... via the json_encode of the array_values of the ACF group array) | |
$social_links_code .= json_encode(array_values($social_links)); | |
} | |
$json_content = '{ | |
"@context": "https://schema.org/", | |
"@type": "Person", | |
"@id": '. $team_member_id .', | |
"name": '. $team_member_name .', | |
'. $team_member_position_code .' | |
'. $team_member_description_code .' | |
"worksFor": | |
{ | |
"@type": "Organization", | |
"name": "FreshySites", | |
"sameAs": [ | |
"https://www.facebook.com/FreshySites/", | |
"https://twitter.com/FreshySites", | |
"https://www.linkedin.com/company/freshysites/", | |
"https://www.youtube.com/user/FreshySites", | |
"https://www.instagram.com/freshysites/", | |
"https://www.pinterest.com/FreshySites/" | |
] | |
}, | |
'. $team_member_image_code .' | |
"url": '. $team_member_url .''. $team_member_url_comma .' | |
'. $social_links_code .' | |
}'; | |
// BEGIN THE OUTPUT | |
$the_json_data = '<script type="application/ld+json">' . $json_content . '</script>'; | |
// for testing raw output | |
//$the_json_data = '<pre>' . $json_content . '</pre>'; | |
return $the_json_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment