Support

Home Forums Event Espresso Premium ld+json customization

ld+json customization

Posted: November 13, 2019 at 12:09 pm


Daniel

November 13, 2019 at 12:09 pm

Hi!

I tried moving the file:
event-espresso-core-reg/core/templates/json_linked_data_for_event.template.php

into my child theme root to apply an override (as is the custom with other EE templates).

However, it doesn’t seem to be overriding the ld+json output.

Is this expected behavior?
Is there a way to customize this template?

Thanks for any insight here! Also, feel free to tell me if there is a better way to customize the ld+json schema. Specifically I want to add “performer” to the schema output and include the people add-on data that is related to the event.


Josh

  • Support Staff

November 13, 2019 at 1:24 pm

Hi,

The core plugin doesn’t actually check in the theme for the json_linked_data_for_event.template.php, it can only load that template from core plugin.

One way the output can be customized is by using the
AHEE__json_linked_data_for_event__template
action hook. The hook can be used to add more to the original output.

You can also filter the template args with this filter hook:
FHEE__EEH_Schema__add_json_linked_data_for_event__template_args

If your customizations require more than the hooks can provide, you could remove the ld+json output and replace with your own. EE hooks into the wp_print_scripts hook to add the ld+json output. The following code will unhook it:

add_action('wp_print_scripts', 'my_custom_callback_print_scripts', 9);
function my_custom_callback_print_scripts() {
    $Front_Controller = EE_Registry::instance()->load_core(
        'Front_Controller', array(), false
    );
    remove_action(
        'wp_print_scripts',
        array($Front_Controller, 'wp_print_scripts'),
        10
    );
}

Then you can add your own custom callback function onto wp_print_scripts to load in your custom output.


Daniel

November 13, 2019 at 1:51 pm

Excellent! I can definitely move forward with those options.
I am pleased with how customizable the EE system is. It is vast … so your guidance is much appreciated.


Josh

  • Support Staff

November 13, 2019 at 2:49 pm

Cool, please let us know if you need further help with this.


Daniel

November 15, 2019 at 6:43 am

I ended up doing this with the most straightforward add_action.

I am not super happy about the global $post (low wp foo right now) so any insights about how to get rid of that would be great!


// add performer json with people add-on data
function add_ld_json () {
	global $post;
	// add comma and performer
	$people = EEM_Person::instance()->get_people_for_event_and_type($event_id = $post->ID, $people_type_id = 8);

	if (count($people) > 0) {
		$full_name = '';
		foreach ( $people as $type => $person ) {
			$full_name .= $person->full_name();
		}
	} else {
		$full_name = 'TBD';
	}

	?>,
	"performer": {
                    "@type": "Person",
                    "name": "<?php echo $full_name; ?>"
                }
	<?php
}
add_action( 'AHEE__json_linked_data_for_event__template', 'add_ld_json');


Josh

  • Support Staff

November 15, 2019 at 7:36 am

Hi,

That looks great!

You can use get_the_ID() there instead.


Daniel

November 15, 2019 at 8:40 am

I’ve improved the code. Thanks for the suggestion.
It correctly supports multiple performers in the schema:


// add performer json with people add-on data
function add_ld_json () {
	$people = EEM_Person::instance()->get_people_for_event_and_type($event_id = get_the_id(), $people_type_id = 8);
	$performer_array = array();
	if (count($people) > 0) {
		foreach ( $people as $person ) {
			$featured_img_url = get_the_post_thumbnail_url($person->ID(),'thumbnail');
			$full_name = trim($person->full_name());
			$performer_array[] = '{
                "@type": "Person",
                "name": "'. $full_name .'",
                "image": "'. $featured_img_url .'"
            }';
		}
	} else {
		$performer_array[] = '{
            "@type": "Person",
            "name": "TBD",
            "image": ""
        }';
	}
	$performer_block = "[". implode(',', $performer_array) ."]";

	?>,
	"performer": <?php echo $performer_block; ?>
	<?php
}
add_action( 'AHEE__json_linked_data_for_event__template', 'add_ld_json');

The support post ‘ld+json customization’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso