Dev.to AI 🤖 Ai 👁 0 📖 4 min read

How to Create a Voice-Powered WordPress Plugin and Host It

From Voice‑to‑WordPress: Build a Voice‑Powered Plugin in a Day If you’re a WordPress developer who loves experimenting with voice AI, you’ve probably already heard about ElevenLabs. They’ve built a robust API that turn

From Voice‑to‑WordPress: Build a Voice‑Powered Plugin in a Day

If you’re a WordPress developer who loves experimenting with voice AI, you’ve probably already heard about ElevenLabs. They’ve built a robust API that turns text into high‑fidelity speech and even lets you clone a voice with a few samples. The best part? You can wrap everything in a tiny plugin and deploy it to a cheap, reliable host like Bluehost. In this post, we’ll walk through the whole process—from setting up your ElevenLabs credentials to creating a user‑friendly plugin and finally getting it live on Bluehost.

1. Why Voice AI on WordPress?

  • Accessibility: Let screen readers and voice assistants read your content in a natural, expressive tone.
  • Engagement: Turn blog posts into podcasts on the fly.
  • SEO: Voice search is booming; having audio can improve your visibility.
  • Fun: Imagine a “Read Aloud” button that speaks the article in your own voice.

2. Get Your ElevenLabs API Key

  1. Sign up at the official ElevenLabs site.
  2. Navigate to API Keys in your dashboard.
  3. Copy the key and keep it safe—this is what your plugin will use to talk to the service.

Tip: Store the key as a WordPress option or in wp-config.php to keep it out of your codebase.

3. Create the Plugin Skeleton

# Inside wp-content/plugins
mkdir voice-powered
cd voice-powered
touch voice-powered.php

Add the header and enqueue a script for the front‑end UI:

<?php
/**
 * Plugin Name: Voice Powered
 * Description: A lightweight plugin that turns your posts into audio using ElevenLabs.
 * Version: 1.0
 * Author: Your Name
 */

if ( ! defined( 'ABSPATH' ) ) exit;

// Enqueue JS
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_script( 'vp-frontend', plugins_url( 'js/vp-frontend.js', __FILE__ ), [ 'jquery' ], null, true );
    wp_localize_script( 'vp-frontend', 'VP', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ] );
} );

// Add a shortcode that renders a "Listen" button
add_shortcode( 'listen_button', function() {
    return '<button class="vp-listen-btn">Listen</button>';
} );

// AJAX handler that calls ElevenLabs
add_action( 'wp_ajax_nopriv_vp_generate_audio', 'vp_generate_audio' );
add_action( 'wp_ajax_vp_generate_audio', 'vp_generate_audio' );
function vp_generate_audio() {
    $post_id = intval( $_POST['post_id'] );
    $post    = get_post( $post_id );

    // Call ElevenLabs
    $api_key = get_option( 'vp_elevenlabs_key' ); // Store this in Settings
    $response = wp_remote_post( 'https://api.elevenlabs.io/v1/text-to-speech', [
        'headers' => [
            'xi-api-key' => $api_key,
            'Content-Type' => 'application/json',
        ],
        'body' => wp_json_encode( [
            'text' => $post->post_content,
            'voice' => 'alloy', // choose a voice or allow user selection
            'model_id' => 'eleven_monolingual_v1',
        ] ),
    ] );

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

    // Return the audio URL (ElevenLabs streams the audio)
    wp_send_json_success( [
        'audio_url' => wp_remote_retrieve_header( $response, 'location' ),
    ] );
}

Heads‑up: ElevenLabs streams the audio via a temporary URL. You can store the URL or cache it in a transient if you want to reduce repeated API calls.

4. Front‑End JavaScript

Create js/vp-frontend.js:

jQuery(document).ready(function ($) {
    $('.vp-listen-btn').on('click', function () {
        var postId = $(this).closest('.post').data('post-id'); // adjust selector as needed
        var button = $(this);

        button.prop('disabled', true).text('Generating…');

        $.post(VP.ajaxurl, {
            action: 'vp_generate_audio',
            post_id: postId,
        }, function (res) {
            if (res.success) {
                var audio = new Audio(res.data.audio_url);
                audio.play();
                button.text('Play again');
            } else {
                alert('Error: ' + res.data);
            }
            button.prop('disabled', false);
        });
    });
});

Now, just drop [listen_button] into any post, and you’re ready to go.

5. Adding Voice Cloning (Optional)

ElevenLabs also offers voice cloning. The workflow is similar: upload a few minutes of audio, then use the resulting voice_id in the TTS call. Add a simple admin screen to upload a voice sample and generate a clone ID.

add_action( 'admin_menu', function() {
    add_menu_page( 'Voice Cloning', 'Voice Cloning', 'manage_options', 'vp-voice-clone', 'vp_clone_page' );
} );

function vp_clone_page() {
    // Simple form to upload a file and trigger cloning
}

Keep the cloning logic to a separate function, and store the returned voice_id in the options table. Then, in the AJAX handler, replace 'voice' => 'alloy' with that voice_id.

6. Deploying to Bluehost

Once you’re happy with the plugin, zip it and upload via the WordPress plugin screen—or just drop it into the wp-content/plugins folder on your Bluehost server. Bluehost is an excellent choice for deploying voice‑AI projects:

  • Easy Setup: One‑click WordPress installs.
  • Affordable Plans: Shared hosting starts at just a few dollars a month.
  • Reliable Performance: SSD storage and 24/7 support mean your audio will load fast for visitors.

If you’re looking for a hassle‑free way to get your plugin live, Bluehost is the go‑to host. It’s straightforward, budget‑friendly, and integrates seamlessly with WordPress.

7. Performance Tips

  • Cache the Audio: Use WordPress transients to store the audio URL for a few hours.
  • Lazy Load: Only request audio when the user clicks the button.
  • Compress Audio: ElevenLabs already does this, but you can set the audio_format to mp3 or wav as needed.
  • Monitor API Usage: ElevenLabs has rate limits; keep an eye on your usage via their dashboard.

8. Security Checklist

  • Never expose your API key in the public code.
  • Validate user input (post_id) before processing.
  • Use wp_nonce_field if you add a settings page.
  • Sanitize outputs with esc_url_raw or esc_html.

9. Wrap‑Up

You now have a fully functional WordPress plugin that turns any post into a high‑quality audio experience, powered by ElevenLabs. You’ve also learned how to host it affordably on Bluehost, ensuring your visitors get a smooth, fast experience.

Ready to give your site a voice? Sign up for ElevenLabs at https://try.elevenlabs.io/kr07zfuqn1bp and launch your WordPress project on Bluehost at https://bluehost.sjv.io/5k0d52. Let’s bring your content to life!

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.