WordPress Integration

Ekoo widget on a WordPress or WooCommerce site: shortcode, theme template, Gutenberg block or script plugin, with the transcript rendered in PHP.

The widget auto-loads its config from the backoffice. Appearance attributes are optional overrides — no need to set them if the backoffice is configured.
Core attributes
data-ekooReq
string
Your Ekoo website UUID: backoffice, Settings → Organization, Organization ID field.
data-ekoo-product-idReq
string
Product reference (must match exactly what is in your Ekoo catalog).
data-ekoo-locale
string·default: auto
Locale code: fr, en, es, it, de, ar, cn, tw, hk, jp, kr, nl, tr, pl, pt, lu, be, ru. "auto" = navigator.language detection.
data-ekoo-variant
string
Stable reference of a widget configuration. The product, its audios, and its reviewers still come from data-ekoo-product-id; only the widget styling changes.
data-ekoo-review-id
string
ID of a specific audio review. If omitted: the product's default audio, otherwise the first published audio.
data-ekoo-on-event
string (fn name)
Name of a global window function called on each widget event (printed, played-0, played-25…).
Appearance — backoffice overrides
data-ekoo-direction
normal | reverse·default: normal
Expansion direction. normal = left-to-right, reverse = right-to-left.
data-ekoo-scale
number·default: 1
Scale factor (e.g. "1.2" for 20% larger).
data-ekoo-animation
string·default: pulse
Animation type for the widget icon at rest.
data-ekoo-animation-duration
string·default: continuous
Animation duration.
data-ekoo-always-open
boolean·default: false
If "true", the widget stays permanently expanded.
data-ekoo-show-image
boolean·default: true
Show or hide the product image in the widget.
data-ekoo-not-fully-clickable
boolean·default: false
If "true", only the play button is clickable.
data-ekoo-autoplay
boolean·default: false
Automatically start audio playback on load.
data-ekoo-audio-preload
metadata | none·default: metadata
Standalone 4.0.0: overrides the backoffice setting. “none” waits until the first click to load the file and disables autoplay.
data-ekoo-show-transcript
boolean·default: false
Show a button to read the audio transcript.
data-ekoo-show-speed-button
boolean·default: false
Show a playback speed control.
data-ekoo-closed-state-main-text
string
Main CTA text shown when the widget is collapsed.
data-ekoo-closed-state-secondary-text
string
Secondary text below the CTA when the widget is collapsed.
SPA & Shadow DOM
data-ekoo-mode
spa | static·default: auto
Forces rendering mode. Auto-detected (Next.js, Nuxt, React, Vue, Angular, Sapper). Only use if auto-detection fails.
data-shadow-mode
open | closed·default: open
"open" (default) enables inspection, external CSS access and analytics tracking. Set to "closed" to fully isolate the widget.
Global JS config
window.EKOO_FORCE_SPA = true
Forces SPA mode globally (alternative to data-ekoo-mode="spa" on each widget).
window.ekooShadowMode = "open"
Global Shadow DOM mode (alternative to data-shadow-mode on each widget).

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:

functions.php — Enqueue script
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.php file, or to a code snippets plugin.

Three values to prepare

  1. Website ID: in the backoffice, Settings → Organization, Organization ID field, or in the data-ekoo attribute of the widget already installed.
  2. Product reference: exactly the one in data-ekoo-product-id on your site and in Ekoo (ID or SKU), without changing convention.
  3. Language: an explicit value such as fr or en, the same as data-ekoo-locale. Never auto.

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.

functions.php
php
1<?php
2// functions.php
3// 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';
7
8// 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 }
15
16 $url = 'https://app.ekoo.co/ws/websites/' . rawurlencode( $website_id )
17 . '/products/' . rawurlencode( $product_ref )
18 . '?' . http_build_query( array( 'locale' => $locale ) );
19
20 $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 }
25
26 $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 }
31
32 // HTML cached for 15 minutes and regenerated in the background.
33 set_transient( $cache_key, $body, 15 * MINUTE_IN_SECONDS );
34 return $body;
35}
36
37// 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}
49
50// 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}
58
59// 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'] ?? '' ) );
64
65 $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 );
77
78 // 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_UNICODE
82 );
83}
84
85// 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 }
99
100 // 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_attr
107 );
108}
109
110// 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:

Shortcode
text
1[ekoo_widget locale="en"]
2[ekoo_widget product_id="SKU-1234" locale="en"]

Or directly in a classic theme template:

content-single-product.php (WooCommerce)
php
1<?php
2// 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

  1. Transcript in the source code: view the page source (Ctrl+U), look for data-ekoo-audio-id and a sentence of the transcript.
  2. Playback: the player shows up and the audio starts on click.
  3. 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:

functions.php — Shortcode
php
1function ekoo_widget_shortcode( $atts ) {
2 $atts = shortcode_atts( array(
3 'website_id' => '',
4 'product_id' => '',
5 'locale' => '',
6 ), $atts, 'ekoo_widget' );
7
8 if ( empty( $atts['website_id'] ) || empty( $atts['product_id'] ) ) {
9 return '<!-- Ekoo: website_id and product_id are required -->';
10 }
11
12 $locale_attr = ! empty( $atts['locale'] )
13 ? ' data-ekoo-locale="' . esc_attr( $atts['locale'] ) . '"'
14 : '';
15
16 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_attr
21 );
22}
23add_shortcode( 'ekoo_widget', 'ekoo_widget_shortcode' );

Use it in any post or page:

Shortcode usage
text
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:

Gutenberg Custom HTML block
html
1<ekoo-widget
2 data-ekoo="YOUR_WEBSITE_ID"
3 data-ekoo-product-id="YOUR_PRODUCT_ID"
4 data-ekoo-locale="en"
5></ekoo-widget>
6
7<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:

WooCommerce product template
php
1<?php
2global $product;
3if ( $product ) :
4?>
5<ekoo-widget
6 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:

Using WordPress post ID
php
1<ekoo-widget
2 data-ekoo="YOUR_WEBSITE_ID"
3 data-ekoo-product-id="<?php echo esc_attr( get_the_ID() ); ?>"
4></ekoo-widget>
💡

WooCommerce integration

If you are using WooCommerce, you can use the product ID directly as your Ekoo product identifier — just make sure it matches what you have configured in the Ekoo backoffice.

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:

Script tag for plugin
html
1<script src="https://app.ekoo.co/widgets/widget-4.0.0-standalone.js"></script>

Best Practices

  • Enqueue the script using wp_enqueue_script rather 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.