title: How can i add a JSON script (Schema data) to the head of each post's page once the post is saved on wordpress
body: <p>I wrote a php snippet that accesses data in the database each time a post is saved or created and stores it in a string variable using the hook add_action(&quot;wp_insert_post&quot;,...) to produce a script in google rich format. I'm however facing difficutlties in adding the script corresponding to each post in their respective web pages. For length reasons i have added a shorter version of my code.</p>
  <pre><code>add_action( 'wp_insert_post','post_update', 10,3);
  
  function post_update( $post_id, $post, $update ){
  
  $post = get_post($post_id);
     
  $output.='
  &lt;script type=&quot;application/ld+json&quot;&gt;
  {
      &quot;@context&quot;: &quot;https://schema.org&quot;,
      &quot;@type&quot;: &quot;Event&quot;,'; 
  
  
  $postMeta= get_post_meta(get_the_ID());
  
  $StartTime = $postMeta['event_start_time'][0];
  $EndTime = $postMeta['event_end_time'][0];
  $StartDate= $postMeta['event_start_date'][0];
  $EndDate = $postMeta['event_end_date'][0];
  
  if (!empty($StartTime) &amp;&amp; !empty($EndTime)){
      $StartDate=$StartDate.&quot;T&quot;.$StartTime.&quot;:00&quot;;
      $EndDate=$EndDate.&quot;T&quot;.$EndTime.&quot;:00&quot;;
  }
  
  $EventName= !empty( $post-&gt;post_title ) ? $post-&gt;post_title : '';
  
  $output.= '
      &quot;name&quot;: &quot;'.$EventName.'&quot;,
      &quot;image&quot;: &quot;......&quot;
      &quot;startDate&quot;: &quot;'.$StartDate.'&quot;,
      &quot;endDate&quot;: &quot;'.$EndDate.'&quot;,
   }
  &lt;/script&gt;';
   }
  </code></pre>
  <p>How can i add the variable output to the pages belonging to the corresponding post (which are events in this case)</p>