Firing schema via code in functions.php doesn't work

admin2025-01-07  5

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.

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.

Share Improve this question edited Apr 21, 2020 at 9:46 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Nov 26, 2019 at 2:10 civixscivixs 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264037a936.html

最新回复(0)