I have meta fields containg json schema mark up for products. I want to add these to the footer. I have tried using this function
add_action('wp_footer', 'insert_my_page_schema');
function insert_my_page_schema() {
if ( ! is_product() ) {
return;
}
$schema = get_post_meta( get_the_ID(), 'schema', true);
if ( ! $schema ) {
return;
}
echo $schema;
}
but this adds the code into the frontend, visible for the viewer of the site. How can I add mycustom field values inline to the footer, so that it is only visible for Google/in the source code, but not in frontend?
CodePudding user response:
It can be issue with wrapping schema code with <script type="application/ld json"></script>
tag, if you want to add schema then must need to wrap script tag as per this google reference example - https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
You may achieve this using anyone of below option
- Wrap
<script type="application/ld json">{your schema code here}</script>
in scheme field value from backend - You can wrap script tag in code itself
echo '<script type="application/ld json">'. $schema .'</script>';
instead ofecho $schema;