Last active
July 24, 2026 21:19
-
-
Save westonruter/763ee32cdc2ed1ad063b261d2ac3772e to your computer and use it in GitHub Desktop.
Prepends the widget to all post content on the single article view. This eliminates the need for the WPCode as included in the [documentation](https://elevenlabs.io/docs/eleven-creative/audio-tools/audio-native/word-press). It also improves performance in how the script is loaded.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * ElevenLabs Audio Native Widget Plugin for WordPress | |
| * | |
| * @package WestonRuter\ElevenLabs\AudioNative | |
| * @author Weston Ruter | |
| * @license GPL-2.0-or-later | |
| * @copyright Copyleft 2026, Weston Ruter | |
| * | |
| * @wordpress-plugin | |
| * Plugin Name: ElevenLabs Audio Native Widget | |
| * Plugin URI: https://gist.github.com/westonruter/763ee32cdc2ed1ad063b261d2ac3772e | |
| * Description: Prepends the widget to all post content on the single article view. This eliminates the need for the WPCode as included in the <a href="https://elevenlabs.io/docs/eleven-creative/audio-tools/audio-native/word-press">documentation</a>. It also improves performance in how the script is loaded. | |
| * Requires at least: 6.9 | |
| * Requires PHP: 7.4 | |
| * Version: 0.1.1 | |
| * Author: Weston Ruter | |
| * Author URI: https://weston.ruter.net/ | |
| * License: GPLv2 or later | |
| * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
| * Update URI: https://gist.github.com/westonruter/763ee32cdc2ed1ad063b261d2ac3772e | |
| * Gist Plugin URI: https://gist.github.com/westonruter/763ee32cdc2ed1ad063b261d2ac3772e | |
| * Primary Branch: main | |
| */ | |
| namespace WestonRuter\ElevenLabs\AudioNative; | |
| use WP_Scripts; | |
| const SCRIPT_HANDLE = 'elevenlabs-audionative-helper'; | |
| /** | |
| * Registers the script. | |
| */ | |
| function register_script( WP_Scripts $scripts ): void { | |
| $scripts->add( | |
| SCRIPT_HANDLE, | |
| 'https://elevenlabs.io/player/audioNativeHelper.js', | |
| array(), | |
| null, | |
| array( | |
| 'in_footer' => true, | |
| 'strategy' => 'defer', | |
| ) | |
| ); | |
| } | |
| add_action( 'wp_default_scripts', __NAMESPACE__ . '\register_script' ); | |
| /** | |
| * Filters the content to prepend the widget on single article pages, while also enqueueing the script. | |
| */ | |
| function filter_content( $content ): string { | |
| if ( ! is_string( $content ) ) { | |
| $content = ''; | |
| } | |
| if ( ! is_single() ) { | |
| return $content; | |
| } | |
| $player = <<<'PLAYER' | |
| <div | |
| id="elevenlabs-audionative-widget" | |
| data-height="90" | |
| data-width="100%" | |
| data-frameborder="no" | |
| data-scrolling="no" | |
| data-publicuserid="public-user-id" | |
| data-playerurl="https://elevenlabs.io/player/index.html" | |
| data-projectid="project-id" | |
| > | |
| Loading the <a href="https://elevenlabs.io/text-to-speech" target="_blank" rel="noopener">Elevenlabs Text to Speech</a> AudioNative Player... | |
| </div> | |
| PLAYER; | |
| wp_enqueue_script( SCRIPT_HANDLE ); | |
| return $player . "\n\n" . $content; | |
| } | |
| add_filter( 'the_content', __NAMESPACE__ . '\filter_content' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment