• Resolved mountbatt

    (@mountbatt)


    Hi,

    can you help me to add <script type=”application/ld+json”> to the header in a single-event-page? <script> tags get deleted, when I save the edits. So how can I add some stuff like json-ld to the single event output? I want to add special tags for google knowledge graph … see below

    
    <script type="application/ld+json"> 
    {
      "@context": "https://www.schema.org",
      "@type": "Event",
      "name": "#_EVENTNAME",
      "url": "#_EVENTPAGEURL",
      "description": "#_EXCERPT",
      "startDate": "#_STARTDATE{Y-m-d}T#_24HSTARTTIME",
      "endDate": "#_ENDDATE{Y-m-d}T#_24HENDTIME",
      "location": {
        "@type": "Place",
        "name": "#_LOCATION",
        "address": {
          "@type": "PostalAddress",
          "streetAddress": "#_ADDRESS",
          "addressLocality": "#_CITY",
          "postalCode": "#_ZIP",
          "addressCountry": "#_COUNTRY"
        }
      },
    }
     </script>
    
Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Franky

    (@liedekef)

    Go to EME options, tab “Other” and try adding it to “Extra event html headers”. I hope those don’t get stripped (I should check) ??

    Thread Starter mountbatt

    (@mountbatt)

    Thats where i tried it! It gets stripped. Now i am working on a functions.php action …

    I think i get it now with

    
    add_action('wp_head', 'add_jsonld_head', 1);
    function add_jsonld_head() {
    	if ( eme_is_single_event_page() ) {
    		
    		$event_ID = get_the_id();
    		$event = eme_get_event($event_ID);
    
    • This reply was modified 6 years, 1 month ago by mountbatt.
    Thread Starter mountbatt

    (@mountbatt)

    Okay, here is my final code:

    Now the structured data are available to the Google Knowledge Graph and the events will be visible on Maps … yeah ??

    
    add_action('wp_head', 'add_jsonld_head', 1);
    function add_jsonld_head() {
    	if ( eme_is_single_event_page() ) {
    		
    		$event_ID = get_the_id();
    		$event = eme_get_event($event_ID);
    		$location_id = $event['location_id'];
    		$location = eme_get_location(intval($location_id));
    		?>
    		
    		<script type="application/ld+json">
    			{
    			  "@context": "https://www.schema.org",
    			  "@type": "Event",
    			  "name": "<?php echo $event['event_name']; ?>",
    			  "url": "<?php echo get_the_permalink(); ?><?php echo $event['event_slug']; ?>",
    			  "image": [
    				  "<?php echo $event['event_image_url'];?>"
    			  ],
    			  "description": "<?php echo $event['event_notes']; ?>",
    			  "startDate": "<?php echo $event['event_start_date']; ?>T<?php echo $event['event_start_time']; ?>",
    			  "endDate": "<?php echo $event['event_end_date']; ?>T<?php echo $event['event_end_time']; ?>",
    			  "location": {
    			    "@type": "Place",
    			    "name": "<?php echo $location['location_name']; ?>",
    			    "address": {
    			      "@type": "PostalAddress",
    			      "streetAddress": "<?php echo $location['location_address1']; ?>",
    			      "addressLocality": "<?php echo $location['location_city']; ?>",
    			      "postalCode": "<?php echo $location['location_zip']; ?>",
    			      "addressCountry": "<?php echo $location['location_country']; ?>"
    			    }
    			  }
    			}
    		</script>
    		
    		<?php
    	}
    }
    
    
    • This reply was modified 6 years, 1 month ago by mountbatt.
    Ambyomoron

    (@josiah-s-carberry)

    @mountbatt may I ask for clarification about what you are doing? Are you talking about the use case of displaying structured data for an event when you find that event on Google Search? Or are you talking about displaying the event from within your web site?

    Thread Starter mountbatt

    (@mountbatt)

    @josiah-s-carberry I want my events to appear in Google Search or in Google Maps / Places like it is in this example: https://imgur.com/a/OlnAtKL (see on the right)

    My hope is if I add structured event data as json-ld, google will crawl and add them to their index.

    Like it is written here: https://developers.google.com/search/docs/data-types/event

    Plugin Author Franky

    (@liedekef)

    I guess you base yourself on https://developers.google.com/search/docs/data-types/event ?? I hope that’ll work … but I’ll check this evening to allow javascript on the single-event page.
    Also: get_the_id() is not going to work to get the event id (well, it shouldn’t anyway), you’ll need to use
    get_query_var('event_id')

    Thread Starter mountbatt

    (@mountbatt)

    get_query_var(‘event_id’) was not working – so i tried it with get_the_id() and it worked! It would be much simpler if we would be able to put javascript in the extra headers for single-event page.

    You can see my result in the structured data testing tool here:

    https://search.google.com/structured-data/testing-tool/#url=https%3A%2F%2Fwww.koeln-ostheim.de%2Ftermine%2Foeffentliche-versammlung-der-bvo-2%2F

    • This reply was modified 6 years, 1 month ago by mountbatt.
    Plugin Author Franky

    (@liedekef)

    If get_the_id() gives you the event id, that would be a great thing (but is not by me) ??

    Thread Starter mountbatt

    (@mountbatt)

    Hmm, let me check on that … maybe i was not seeing it correctly – damn ?? Will get back in a minute.

    Thread Starter mountbatt

    (@mountbatt)

    Yes you are right – all events now have the same json-ld data from my first event in the calendar. Oh damn sh*t.

    Thread Starter mountbatt

    (@mountbatt)

    Haha, the id of the event i was testing with was 22, and the id of my events-list-page was 22 too.

    But now get_query_var(‘event_id’) gives me only the slug of my event.

    Plugin Author Franky

    (@liedekef)

    That is not an issue, eme_get_event() can handle that.

    Thread Starter mountbatt

    (@mountbatt)

    Yes, it works ??
    Thanks for your fast responses!

    https://search.google.com/structured-data/testing-tool/#url=https%3A%2F%2Fwww.koeln-ostheim.de%2Ftermine%2Foeffentliche-versammlung-der-bvo-2%2F

    Final Code in functions.php:

    
    add_action('wp_head', 'add_jsonld_head', 1);
    function add_jsonld_head() {
    	if ( eme_is_single_event_page() ) {
    		$event_ID = get_query_var('event_id');
    		$event = eme_get_event($event_ID);
    		$location_id = $event['location_id'];
    		$location = eme_get_location(intval($location_id));
    		?>
    		<script type="application/ld+json">
    			{
    			  "@context": "https://www.schema.org",
    			  "@type": "Event",
    			  "name": "<?php echo $event['event_name']; ?>",
    			  "url": "<?php echo get_the_permalink(); ?><?php echo $event['event_slug']; ?>",
    			  "image": [
    				  "<?php echo $event['event_image_url'];?>"
    			  ],
    			  "description": "<?php echo $event['event_notes']; ?>",
    			  "startDate": "<?php echo $event['event_start_date']; ?>T<?php echo $event['event_start_time']; ?>",
    			  "endDate": "<?php echo $event['event_end_date']; ?>T<?php echo $event['event_end_time']; ?>",
    			  "location": {
    			    "@type": "Place",
    			    "name": "<?php echo $location['location_name']; ?>",
    			    "address": {
    			      "@type": "PostalAddress",
    			      "streetAddress": "<?php echo $location['location_address1']; ?>",
    			      "addressLocality": "<?php echo $location['location_city']; ?>",
    			      "postalCode": "<?php echo $location['location_zip']; ?>",
    			      "addressCountry": "<?php echo $location['location_country']; ?>"
    			    }
    			  }
    			}
    		</script>
    		<?php
    	}
    }
    
    
    Plugin Author Franky

    (@liedekef)

    Great, now I’ll check on the javascript thing this weekend.

    Plugin Author Franky

    (@liedekef)

    The next version will allow javascript for “Extra event html headers” and “Extra location html headers”

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How to add json-ld to header’ is closed to new replies.