How to Use ElevenLabs with WordPress
Why Voice AI Is a Game‑Changer for WordPress If you’ve ever built a site that serves up tutorials, podcasts, or accessibility features, you know how powerful a clear, natural voice can be. Text‑to‑speech (TTS) engines
Why Voice AI Is a Game‑Changer for WordPress
If you’ve ever built a site that serves up tutorials, podcasts, or accessibility features, you know how powerful a clear, natural voice can be. Text‑to‑speech (TTS) engines have gone from robotic monotone to near‑human‑like speech in the last few years. For WordPress developers, that means you can add a “Listen to this post” button, create audio versions of your articles, or even clone your own voice to give your brand a unique auditory identity—all with a few lines of code.
The key to making this happen quickly is choosing a TTS provider that’s both high‑quality and developer‑friendly. That’s where ElevenLabs comes in. With its deep‑learning models, fine‑grained voice controls, and straightforward REST API, it lets you generate realistic speech in under a second. Below is a step‑by‑step guide to hooking ElevenLabs into a WordPress site, from getting an API key to embedding the audio in posts.
Getting Started with ElevenLabs
1. Create an ElevenLabs Account
Head over to the official ElevenLabs sign‑up page and register for a free tier. Once you’re logged in, you’ll find your API key under Account Settings > API Keys. Keep this key handy; you’ll need it for all API calls.
Tip: If you want to try out the platform before committing, use the affiliate link below. It gives you a free trial and a small discount for new users.
https://try.elevenlabs.io/kr07zfuqn1bp
2. Install the WordPress Plugin Boilerplate
You can embed the audio directly in posts via a shortcode, or you can use a more advanced approach that stores the audio file on your server. For this tutorial, we’ll use a simple plugin skeleton that registers a shortcode called [elevenlabs].
# Create a new plugin folder
mkdir wp-elevenlabs
cd wp-elevenlabs
touch elevenlabs.php
Add the following header to elevenlabs.php:
<?php
/**
* Plugin Name: ElevenLabs TTS
* Description: Generates and embeds natural-sounding speech from post content.
* Version: 1.0
* Author: Your Name
*/
Now we’ll hook into WordPress to add the shortcode.
add_shortcode('elevenlabs', 'elevenlabs_generate_audio');
function elevenlabs_generate_audio($atts) {
// Extract shortcode attributes
$atts = shortcode_atts(array(
'voice' => 'male', // default voice ID
'text' => ''
), $atts, 'elevenlabs');
// If text isn’t provided, use the current post content
$text = $atts['text'] ?: get_the_content();
// Call the ElevenLabs API
$audio_url = generate_audio_from_text($text, $atts['voice']);
// Return an <audio> element
return '<audio controls src="' . esc_url($audio_url) . '"></audio>';
}
Calling the ElevenLabs API
A. Using cURL (PHP)
function generate_audio_from_text($text, $voice_id) {
$api_key = 'YOUR_ELEVENLABS_API_KEY';
$endpoint = 'https://api.elevenlabs.io/v1/text-to-speech/' . $voice_id . '/stream';
$headers = [
"xi-api-key: $api_key",
"Content-Type: application/json"
];
$payload = json_encode([
'text' => $text,
'voice_settings' => [
'stability' => 0.75,
'similarity_boost' => 0.75
]
]);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Save the binary data to a file
$file_path = WP_CONTENT_DIR . '/uploads/elevenlabs_' . uniqid() . '.mp3';
file_put_contents($file_path, $response);
// Return public URL
return content_url('/uploads/' . basename($file_path));
}
B. Using JavaScript (Fetch API)
If you prefer to generate audio client‑side (e.g., for a single‑page app), you can use fetch. Note that you’ll need to hide your API key on the server side; this example is for demonstration only.
async function generateAudio(text, voiceId) {
const response = await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}/stream`, {
method: 'POST',
headers: {
'xi-api-key': 'YOUR_ELEVENLABS_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text,
voice_settings: { stability: 0.75, similarity_boost: 0.75 }
})
});
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.play();
}
Embedding Audio in WordPress Posts
Once you have the shortcode registered, you can add it to any post or page:
[elevenlabs voice="female" text="Welcome to my blog!"]
If you leave the text attribute blank, the plugin will automatically use the post content. You can also customize the voice ID to match the specific voice you created in your ElevenLabs dashboard.
Pro Tip: ElevenLabs offers dozens of pre‑built voices (male, female, regional accents, etc.). Explore the dashboard to find the one that best fits your brand’s tone.
Advanced Features
1. Voice Cloning
ElevenLabs allows you to clone a voice with just a few minutes of audio. Upload a recording of your own voice (or a celebrity voice, if you have the rights), and the model will produce a synthetic voice that sounds just like you.
# Example: Clone a voice using the API
curl -X POST "https://api.elevenlabs.io/v1/voices" \
-H "xi-api-key: YOUR_ELEVENLABS_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/your_voice.wav" \
-F "name=My Custom Voice"
Once the voice is cloned, you can reference it in your shortcode:
[elevenlabs voice="my-custom-voice"]
2. Speed and Pitch Controls
The voice_settings object accepts parameters like stability, similarity_boost, pitch, and rate. Adjusting these lets you fine‑tune the speech to match your content style.
'voice_settings' => [
'stability' => 0.65,
'similarity_boost' => 0.8,
'pitch' => 0, // -10 to +10
'rate' => 0 // -10 to +10
]
3. Caching Generated Audio
Generating audio on every page load can be costly. Store the MP3 files in the wp-content/uploads directory and reuse them for future requests. Use a simple cache key based on the post ID and text hash.
$cache_key = md5($text) . '_' . $voice_id;
$cached_url = wp_cache_get($cache_key);
if ($cached_url) {
return $cached_url;
}
// ... generate and cache as above
wp_cache_set($cache_key, $audio_url, '', 3600); // cache for 1 hour
Troubleshooting Common Issues
| Issue | Symptom | Fix |
|---|---|---|
| 401 Unauthorized | API returns 401 | Double‑check your API key and that it’s in the request header. |
| Large audio file | Audio cuts off or is too long | ElevenLabs limits stream length; split long text into chunks. |
| Latency | Audio takes >5s to play | Use the stream endpoint for real‑time streaming; avoid downloading the entire file before playing. |
| Unsupported voice | Voice ID not found | Verify the voice ID in the ElevenLabs dashboard; use the correct ID in the shortcode. |
Bringing It All Together
- Sign up at ElevenLabs and grab your API key (or use the affiliate link for a free trial). https://try.elevenlabs.io/kr07zfuqn1bp
- Create a simple WordPress plugin that registers a shortcode.
- Call the ElevenLabs API from PHP (or JavaScript) to generate MP3s.
-
Embed the audio in posts using
[elevenlabs]. - Fine‑tune voice settings, clone your own voice, and cache the output for performance.
With these steps, you can transform static blog posts into engaging audio experiences that cater to mobile users, people with visual impairments, or anyone who prefers listening to reading.
Ready to Amplify Your Content?
ElevenLabs offers some of the most natural‑sounding TTS voices available today, and the integration is straightforward for any WordPress developer. Try it out now with the affiliate link below to receive a free trial and see how quickly you can turn words into high‑quality audio.
Give your readers a voice—literally.
https://try.elevenlabs.io/kr07zfuqn1bp
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.