1

I've been banging my head against the wall trying to get schema figured out, so if anyone can help me, I'd appreciate it greatly.

I'm trying to manually add my schema via custom fields, and then firing the schema via a function in my child theme's functions.php.

Well,

When I input this code

    <?php
    $schema = get_post_meta(get_the_ID(), 'schema', true);
    if(!empty($schema)) {
      echo $schema;
    }
    ?>

into my functions.php, I'm met with this error.

Your PHP code changes were rolled back due to an error on line 27 of file wp-content/themes/astra-child/functions.php. Please fix and try saving again.

syntax error, unexpected '<', expecting end of file

I then realized that the code needs to be in my header.php file, but I don't have a header.php file in my child theme, and copying that content over into a new header.php file in my child theme may not be ideal, as when the creator updates their theme, I wouldn't get the benefits of their updated code.

How would I go about resolving this issue? All I want to do is fire my schema via custom fields.

Also, is it a problem that I have different schemas on each page, but each custom field is named 'schema'?

Anyways, I hope this isn't too much to ask. I appreciate any help you can offer.

1 Answer 1

0

The more cleaner way of adding the custom data to your site's <head> is to use a custom function, which you hook to a relevant action. In this case you could for example utilize the wp_head action. Like so,

// this goes to functions.php file
add_action('wp_head', 'insert_my_page_schema');
function insert_my_page_schema() {
  // only execute the code if on a single page
  if ( ! is_page() ) { // modify condition as needed
    return;
  }
  // do we have any saved schema meta
  $schema = get_post_meta( get_the_ID(), 'schema', true);
  if ( ! $schema ) {
    return;
  }
  // add schema markup to the page head
  echo esc_html($schema); // escape output to prevent any unintended html injections
  // or if needed, format_my_page_schema($schema);
}
// optional helper function for formatting the schema output
function format_my_page_schema($schema) {
  // format the schema, if needed
  return $schema;
}

Also, is it a problem that I have different schemas on each page, but each custom field is named 'schema'?

Each page can have its own schema meta data saved with the schema meta key. There's a problem, if you have multiple custom fields for a page with each of them having the same meta key, i.e. every schema input has name="schema". But if the field names are in array format, name="schema[]", and you're saving the data as an array, then there shouldn't be a problem. If the data is saved as an array, then you need to modify the output function to handle the array accordingly.

4
  • Thank you so much for this response. You were remarkably clear, concise, and helpful. I'm running into some issues adding code to my functions.php at the moment, so after I resolve that, I'll try this out and give you a more thorough response depending on what I can figure out. Commented Nov 26, 2019 at 21:05
  • all right, so I added the code to my functions.php as you suggested. Regarding your answer to the second question, since I don't have multiple custom fields on the same page with the same 'schema' name, I didn't need to alter it to an array. Commented Nov 26, 2019 at 21:53
  • However, it literally added the schema markup to the head of my page for the world to see. So, I modified the code and took out the section between '//add schema markup to page head' and '//of if needed"... but now the structured data testing tool doesn't show my schema. I've probably done a newbie mistake here, but what am I missing? Commented Nov 26, 2019 at 22:00
  • @civixs The echo part of my code example probably just needs some refining. In which format are saving the schemas to the custom field? Could you add an example of the meta you've added to the post meta and want to display on the page head to your question, please? Commented Nov 27, 2019 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.