DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

Build a Voice-Enabled Website with ElevenLabs and WordPress

Why Add Voice to Your WordPress Site?

Imagine visitors being able to listen to blog posts while they sip coffee, commute, or work out. Voice‑enabled content is a huge accessibility win, boosts time‑on‑page, and gives your site a futuristic edge. With modern text‑to‑speech (TTS) services you can generate natural‑sounding audio on the fly, and thanks to WordPress’s extensibility you can hook that power right into your posts.

In this guide we’ll build a simple “Read Aloud” button for any post, using:

By the end you’ll have a live WordPress site that can turn any article into a spoken experience with just a click.


What You’ll Need

Item Reason
A Bluehost account (or any WordPress‑compatible host) One‑click WordPress install, SSL, and easy scaling
A fresh WordPress installation The CMS we’ll extend
An ElevenLabs API key To call the TTS endpoint
Basic knowledge of PHP & JavaScript For the plugin and front‑end integration
A code editor & FTP client (or the Bluehost file manager) To upload the plugin files

If you don’t have a host yet, spin up a WordPress site on Bluehost – it��s the easiest, most affordable way to get a production‑ready environment up and running in minutes.


Step 1 – Grab an ElevenLabs API Key

  1. Sign up (or log in) at the ElevenLabs portal: https://try.elevenlabs.io/kr07zfuqn1bp
  2. Navigate to API → Create new secret key.
  3. Copy the key – you’ll need it in the WordPress plugin.

Tip: Keep the key secret! Store it in the wp-config.php file or a dedicated environment variable rather than hard‑coding it.


Step 2 – Create a Simple WordPress Plugin

We’ll wrap the TTS logic in a tiny plugin so you can enable/disable it site‑wide.

  1. In your WordPress wp-content/plugins folder, create a folder called voice‑reader.
  2. Inside that folder, add a file named voice-reader.php:
<?php
/*
Plugin Name: Voice Reader
Description: Adds a "Read Aloud" button to posts using ElevenLabs TTS.
Version: 1.0
Author: Your Name
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Prevent direct access

// 1️⃣ Load the API key from wp-config.php
define( 'ELEVENLABS_API_KEY', defined('ELEVENLABS_API_KEY') ? ELEVENLABS_API_KEY : '' );

// 2️⃣ Enqueue JavaScript
function vr_enqueue_scripts() {
    wp_enqueue_script(
        'voice-reader-js',
        plugins_url( '/js/voice-reader.js', __FILE__ ),
        array( 'jquery' ),
        '1.0',
        true
    );

    // Pass the API key and ajax URL to the script (kept server‑side for security)
    wp_localize_script( 'voice-reader-js', 'vrData', array(
        'ajaxUrl' => admin_url( 'admin-ajax.php' ),
        'nonce'   => wp_create_nonce( 'vr_nonce' )
    ) );
}
add_action( 'wp_enqueue_scripts', 'vr_enqueue_scripts' );

// 3️⃣ Add the button after post content
function vr_add_button( $content ) {
    if ( is_singular( 'post' ) ) {
        $button = '<button id="vr-read-aloud" style="margin-top:1rem;">🔊 Read Aloud</button>';
        return $content . $button;
    }
    return $content;
}
add_filter( 'the_content', 'vr_add_button' );

// 4️⃣ AJAX handler that talks to ElevenLabs
function vr_generate_audio() {
    check_ajax_referer( 'vr_nonce', 'nonce' );

    $text = sanitize_text_field( $_POST['text'] );
    $voice = 'eleven_monolingual_v1'; // default voice, change as needed

    $payload = array(
        'text' => $text,
        'voice_settings' => array(
            'stability' => 0.75,
            'similarity_boost' => 0.85
        )
    );

    $response = wp_remote_post(
        "https://api.elevenlabs.io/v1/text-to-speech/{$voice}",
        array(
            'headers' => array(
                'Authorization' => 'Bearer ' . ELEVENLABS_API_KEY,
                'Content-Type'  => 'application/json'
            ),
            'body'    => wp_json_encode( $payload )
        )
    );

    if ( is_wp_error( $response ) ) {
        wp_send_json_error( $response->get_error_message() );
    }

    // Return the raw audio bytes as a base64 string
    $audio_base64 = base64_encode( wp_remote_retrieve_body( $response ) );
    wp_send_json_success( $audio_base64 );
}
add_action( 'wp_ajax_vr_generate_audio', 'vr_generate_audio' );
add_action( 'wp_ajax_nopriv_vr_generate_audio', 'vr_generate_audio' );
Enter fullscreen mode Exit fullscreen mode
  1. Create a js sub‑folder and add voice-reader.js:
jQuery(document).ready(function($) {
    $('#vr-read-aloud').on('click', async function() {
        const postText = $('article').text().trim(); // crude but works for demo

        // Show a loading state
        $(this).text('⏳ Generating...').prop('disabled', true);

        try {
            const resp = await $.post(vrData.ajaxUrl, {
                action: 'vr_generate_audio',
                nonce: vrData.nonce,
                text: postText
            });

            if (!resp.success) throw new Error(resp.data);

            // Create an audio element from the base64 string
            const audio = new Audio('data:audio/mpeg;base64,' + resp.data);
            audio.play();

            // Reset button
            $(this).text('🔊 Read Aloud').prop('disabled', false);
        } catch (e) {
            alert('Error generating audio: ' + e.message);
            $(this).text('🔊 Read Aloud').prop('disabled', false);
        }
    });
});
Enter fullscreen mode Exit fullscreen mode
  1. Activate the plugin from the WordPress admin → Plugins page.

That’s it! When a visitor clicks Read Aloud, the front‑end sends the article text to a tiny WordPress AJAX endpoint, which forwards the request to ElevenLabs. The returned MP3 stream is turned into a playable audio element.


Step 3 – Test Locally, Then Deploy on Bluehost

Local sanity check

  • Visit any post on your dev site.
  • Click the Read Aloud button.
  • You should hear a smooth, natural voice reading the article.

If you see a CORS error, double‑check that the API key constant is defined and that the wp_remote_post call succeeds. The ElevenLabs API returns raw audio, so the base64 conversion is safe for small‑to‑medium posts (under ~5 KB of text). For longer pieces you might want to generate a temporary file on the server instead.

Deploy with Bluehost

Bluehost makes the jump from local dev to production effortless:

  1. Log in to your Bluehost dashboard.
  2. Choose WordPress → Install and follow the wizard (you’ll get a domain, SSL, and MySQL DB automatically).
  3. Upload the voice-reader plugin via File Manager or FTP to wp-content/plugins.
  4. Activate the plugin and paste your ElevenLabs key into wp-config.php:
define( 'ELEVENLABS_API_KEY', 'YOUR_ELEVENLABS_SECRET' );
Enter fullscreen mode Exit fullscreen mode

Because Bluehost handles PHP updates, caching, and security patches for you, you can focus on building features rather than server maintenance.


Step 4 – Optional Enhancements

Feature How to Add
Voice selection Add a dropdown that sends a different voice ID to the AJAX handler.
Audio caching Store the base64 string in a custom post meta field so the same article isn’t re‑synthesized on every request.
Progress bar Use the fetch API with ReadableStream to stream the audio as it’s generated (great for longer posts).
Accessibility Pair the button with an ARIA label (aria-label="Read this article aloud").

These tweaks turn a prototype into a production‑ready feature that respects both performance and accessibility guidelines.


Wrap‑Up

You now have a fully functional voice‑enabled WordPress site powered by ElevenLabs and hosted on Bluehost. The core idea is simple: capture post content, send it to a world‑class TTS service, and play the result back to the user—all with a few lines of PHP and JavaScript.

If you enjoyed building this, imagine what else you could do:

  • Podcast‑style newsletters generated on demand.
  • Multi‑language versions using ElevenLabs’ language models.
  • Interactive voice assistants that answer comments in real time.

Ready to give your readers the gift of sound? Try ElevenLabs today at https://try.elevenlabs.io/kr07zfuqn1bp and spin up a WordPress site on Bluehost with https://bluehost.sjv.io/5k0d52. Happy coding!

Top comments (0)