WordPress Integration
Ekoo widget on a WordPress or WooCommerce site: shortcode, theme template, Gutenberg block or script plugin, with the transcript rendered in PHP.
data-ekooReqdata-ekoo-product-idReqdata-ekoo-localeautodata-ekoo-variantdata-ekoo-review-iddata-ekoo-on-eventdata-ekoo-directionnormaldata-ekoo-scale1data-ekoo-animationpulsedata-ekoo-animation-durationcontinuousdata-ekoo-always-openfalsedata-ekoo-show-imagetruedata-ekoo-not-fully-clickablefalsedata-ekoo-autoplayfalsedata-ekoo-audio-preloadmetadatadata-ekoo-show-transcriptfalsedata-ekoo-show-speed-buttonfalsedata-ekoo-closed-state-main-textdata-ekoo-closed-state-secondary-textdata-ekoo-modeautodata-shadow-modeopenwindow.EKOO_FORCE_SPA = truewindow.ekooShadowMode = "open"Script Loading
First enqueue the widget script, on every page or only on the pages that use it. Add the following to your theme's functions.php:
1function ekoo_enqueue_widget_script() {2 wp_enqueue_script(3 'ekoo-widget',4 'https://app.ekoo.co/widgets/widget-4.0.0-standalone.js',5 array(),6 '4.0.0',7 array( 'strategy' => 'defer', 'in_footer' => false )8 );9}10add_action( 'wp_enqueue_scripts', 'ekoo_enqueue_widget_script' );WordPress renders your pages on the server. With these functions, the transcript and its JSON-LD are in the initial HTML, with a 15 minute cache and a 2 s timeout. The [ekoo_widget] shortcode keeps the attributes of the shortcode in the “Browser only” tab (website_id, product_id, locale) and replaces it: do not register both. Supply an explicit locale to enable server rendering. With an omitted, empty or “auto” locale, the shortcode keeps the widget alone and its existing language behavior.
Before you start
Requirements
- PHP 7.4 or later.
- Access to your theme’s
functions.phpfile, or to a code snippets plugin.
Three values to prepare
- Website ID: in the backoffice, Settings → Organization, Organization ID field, or in the
data-ekooattribute of the widget already installed. - Product reference: exactly the one in
data-ekoo-product-idon your site and in Ekoo (ID or SKU), without changing convention. - Language: an explicit value such as
froren, the same asdata-ekoo-locale. Neverauto.
Is an A/B experiment comparing the widget with no widget running on these pages? Read the server rendering limits first.
Keep the product reference of your current integration
product_id must be exactly the reference registered in Ekoo and already used on your site. By default, the shortcode takes the WordPress ID of the page; if your integration uses the WooCommerce SKU, pass that SKU.
1. Add the functions
Add this code at the end of functions.php, without the first <?php line if the file already opens with it, then set EKOO_WEBSITE_ID and EKOO_LOCALE at the top of the block.
1<?php2// functions.php3// Public identifier, the same as data-ekoo: not a secret.4const EKOO_WEBSITE_ID = 'YOUR_WEBSITE_ID';5// Same value as data-ekoo-locale. Never 'auto': the server does not know the browser language.6const EKOO_LOCALE = 'en';78// Returns the product, or null when Ekoo has nothing to render (unknown product, error, timeout).9function ekoo_fetch_product( string $website_id, string $product_ref, string $locale ): ?array {10 $cache_key = 'ekoo_product_' . md5( $website_id . '|' . $locale . '|' . $product_ref );11 $cached = get_transient( $cache_key );12 if ( is_array( $cached ) ) {13 return $cached;14 }1516 $url = 'https://app.ekoo.co/ws/websites/' . rawurlencode( $website_id )17 . '/products/' . rawurlencode( $product_ref )18 . '?' . http_build_query( array( 'locale' => $locale ) );1920 $response = wp_remote_get( $url, array( 'timeout' => 2, 'headers' => array( 'Accept' => 'application/json' ) ) );21 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {22 // Timeout or network error: the page renders without the Ekoo block.23 return null;24 }2526 $body = json_decode( wp_remote_retrieve_body( $response ), true );27 // An unknown product answers 200 with { type, message }: test the body, not the status.28 if ( ! is_array( $body ) || isset( $body['type'] ) || ! is_array( $body['reviews'] ?? null ) ) {29 return null;30 }3132 // HTML cached for 15 minutes and regenerated in the background.33 set_transient( $cache_key, $body, 15 * MINUTE_IN_SECONDS );34 return $body;35}3637// Same choice as the widget: unknown explicit ID → first playable audio; no ID → default audio, then first playable.38function ekoo_select_review( array $product, string $review_id = '' ): ?array {39 $playable = array_values(40 array_filter( $product['reviews'], fn( $review ) => ! empty( $review['audio_url'] ) && ! empty( $review['is_active'] ) )41 );42 foreach ( $playable as $review ) {43 if ( '' !== $review_id ? $review['id'] === $review_id : ! empty( $review['is_default'] ) ) {44 return $review;45 }46 }47 return $playable[0] ?? null;48}4950// created_at is a PostgreSQL timestamp ("2026-09-04 15:56:31.998283+00"), not ISO 8601.51function ekoo_upload_date( ?string $value ): ?string {52 try {53 return $value ? ( new DateTimeImmutable( $value ) )->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d\TH:i:s.v\Z' ) : null;54 } catch ( Exception $e ) {55 return null;56 }57}5859// AudioObject JSON-LD: data-ekoo-audio-id tells the widget not to add a second one.60function ekoo_audio_json_ld( array $product, array $review ): string {61 $formats = array( 'mp3' => 'audio/mpeg', 'm4a' => 'audio/mp4', 'ogg' => 'audio/ogg', 'wav' => 'audio/wav' );62 $extension = strtolower( pathinfo( (string) parse_url( $review['audio_url'], PHP_URL_PATH ), PATHINFO_EXTENSION ) );63 $author = trim( ( $review['user']['firstname'] ?? '' ) . ' ' . ( $review['user']['lastname'] ?? '' ) );6465 $data = array(66 '@context' => 'https://schema.org',67 '@type' => 'AudioObject',68 'name' => ( $product['seo_name'] ?? '' ) ?: ( $product['name'] ?? null ),69 'description' => $product['seo_description'] ?? null,70 'contentUrl' => $review['audio_url'],71 'encodingFormat' => $formats[ $extension ] ?? null,72 'inLanguage' => $review['locale'] ?? null,73 'transcript' => str_replace( array( "\r\n", "\r" ), "\n", $review['transcript'] ),74 'uploadDate' => ekoo_upload_date( $review['created_at'] ?? null ),75 'author' => '' !== $author ? array( '@type' => 'Person', 'name' => $author ) : null,76 );7778 // JSON.stringify does not escape "<": a transcript containing "</script>" would close the tag.79 return wp_json_encode(80 array_filter( $data, fn( $value ) => null !== $value && '' !== $value ),81 JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE82 );83}8485// Returns the Ekoo block (JSON-LD and transcript) followed by the widget slot.86function ekoo_render( string $product_ref, string $website_id = EKOO_WEBSITE_ID, string $locale = EKOO_LOCALE ): string {87 $html = '';88 if ( '' !== $locale && 'auto' !== $locale ) {89 $product = ekoo_fetch_product( $website_id, $product_ref, $locale );90 $review = $product ? ekoo_select_review( $product ) : null;91 // An empty or whitespace-only transcript adds nothing.92 if ( $review && 1 === preg_match( '/[^\s\p{Z}]/u', $review['transcript'] ?? '' ) ) {93 $html .= '<script type="application/ld+json" data-ekoo-seo="server" data-ekoo-audio-id="' . esc_attr( $review['id'] ) . '">'94 . ekoo_audio_json_ld( $product, $review ) . '</script>'95 . '<section class="ekoo-transcript"><details><summary>' . esc_html( 'Read the transcript' ) . '</summary>'96 . '<p style="white-space: pre-line">' . esc_html( $review['transcript'] ) . '</p></details></section>';97 }98 }99100 // The widget stays client-side: it becomes interactive once its script loads.101 $locale_attr = '' !== $locale ? ' data-ekoo-locale="' . esc_attr( $locale ) . '"' : '';102 return $html . sprintf(103 '<div data-ekoo="%s" data-ekoo-product-id="%s"%s></div>',104 esc_attr( $website_id ),105 esc_attr( $product_ref ),106 $locale_attr107 );108}109110// An explicit locale enables server rendering; without one, the shortcode keeps the widget alone and its existing language behavior.111add_shortcode(112 'ekoo_widget',113 function ( $atts ) {114 $atts = shortcode_atts(115 array(116 'website_id' => EKOO_WEBSITE_ID,117 'product_id' => (string) get_the_ID(),118 'locale' => '',119 ),120 $atts,121 'ekoo_widget'122 );123 return ekoo_render( $atts['product_id'], $atts['website_id'], $atts['locale'] );124 }125);Caching and PHP without WordPress
Only a valid product is cached: an error (unknown product, timeout, network) is retried on the next render. A product unknown to Ekoo therefore triggers a call on every render: keep the shortcode for products that have Ekoo content, or keep a page cache. In PHP without WordPress, replace wp_remote_get with cURL and the transients with APCu or a file cache.
2. Place the widget
With the shortcode, in a post, a page or the Shortcode block of a block theme:
1[ekoo_widget locale="en"]2[ekoo_widget product_id="SKU-1234" locale="en"]Or directly in a classic theme template:
1<?php2// content-single-product.php (WooCommerce), inside the product loop, after the_post()3// Same reference as data-ekoo-product-id elsewhere on your site (often the SKU): the product ID here, adapt it.4echo ekoo_render( (string) get_the_ID() );Check
- Transcript in the source code: view the page source (Ctrl+U), look for
data-ekoo-audio-idand a sentence of the transcript. - Playback: the player shows up and the audio starts on click.
- Product change: open another product page; the transcript and the audio must match that product.
Detailed checks, including no duplicate JSON-LD: SSR, SEO & GEO guide.
The following methods add the widget alone. The transcript is then only readable once JavaScript has run.
Method 1: Custom Shortcode
Register a shortcode that outputs the widget element. This lets you place the widget anywhere using [ekoo_widget] in the post editor:
1function ekoo_widget_shortcode( $atts ) {2 $atts = shortcode_atts( array(3 'website_id' => '',4 'product_id' => '',5 'locale' => '',6 ), $atts, 'ekoo_widget' );78 if ( empty( $atts['website_id'] ) || empty( $atts['product_id'] ) ) {9 return '<!-- Ekoo: website_id and product_id are required -->';10 }1112 $locale_attr = ! empty( $atts['locale'] )13 ? ' data-ekoo-locale="' . esc_attr( $atts['locale'] ) . '"'14 : '';1516 return sprintf(17 '<div data-ekoo="%s" data-ekoo-product-id="%s"%s></div>',18 esc_attr( $atts['website_id'] ),19 esc_attr( $atts['product_id'] ),20 $locale_attr21 );22}23add_shortcode( 'ekoo_widget', 'ekoo_widget_shortcode' );Use it in any post or page:
1[ekoo_widget website_id="YOUR_WEBSITE_ID" product_id="YOUR_PRODUCT_ID" locale="en"]Method 2: Gutenberg Custom HTML Block
In the Gutenberg editor, add a Custom HTML block and paste the widget element directly:
1<ekoo-widget2 data-ekoo="YOUR_WEBSITE_ID"3 data-ekoo-product-id="YOUR_PRODUCT_ID"4 data-ekoo-locale="en"5></ekoo-widget>67<script src="https://app.ekoo.co/widgets/widget-4.0.0-standalone.js"></script>This approach is quick but requires manually entering the product ID for each page. The shortcode method is more flexible for dynamic content.
Dynamic Product IDs
If you are using WooCommerce, you can output the product ID automatically in your product template. For example, in a WooCommerce single product template:
1<?php2global $product;3if ( $product ) :4?>5<ekoo-widget6 data-ekoo="YOUR_WEBSITE_ID"7 data-ekoo-product-id="<?php echo esc_attr( $product->get_id() ); ?>"8></ekoo-widget>9<?php endif; ?>For standard WordPress posts or pages, you can use the post ID:
1<ekoo-widget2 data-ekoo="YOUR_WEBSITE_ID"3 data-ekoo-product-id="<?php echo esc_attr( get_the_ID() ); ?>"4></ekoo-widget>WooCommerce integration
Plugin Approach
If you prefer not to edit theme files, you can use a plugin such as Header Footer Code Manager or Insert Headers and Footers to add the Ekoo script globally. Simply paste the script tag into the plugin's header section:
1<script src="https://app.ekoo.co/widgets/widget-4.0.0-standalone.js"></script>Best Practices
- Enqueue the script using
wp_enqueue_scriptrather than hardcoding it in the template — this prevents duplicate loading and follows WordPress conventions. - Always escape attribute values with
esc_attr()in PHP templates to prevent XSS vulnerabilities. - Test on both the front end and the Gutenberg editor preview to verify widget placement.
- If you update your WordPress theme, remember to re-add any customizations to
functions.php— or use a child theme to keep changes safe during updates. - Ensure your Ekoo product IDs match the identifiers used in your WordPress or WooCommerce catalog exactly.
Performance
To load the Ekoo script in the <head> (instead of the footer) and improve LCP, use in_footer => false with strategy => 'defer'. See Script loading for the full snippet.