Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from wpcrazydev/text-to-speech.php
Created May 7, 2024 16:44
Show Gist options
  • Save dexit/4cc096ca30d68f1a55a556f6d83c0933 to your computer and use it in GitHub Desktop.
Save dexit/4cc096ca30d68f1a55a556f6d83c0933 to your computer and use it in GitHub Desktop.
Text to speech anywhere in wordpress
/* Function to create TTS shortcode */
function text_to_speech_shortcode($atts) {
$atts = shortcode_atts(
array(
'value' => 'Na', // Default Value
),
$atts
);
$value = $atts['value'];
ob_start();
?>
<button onclick="speakText('<?php echo esc_attr($value); ?>')">🔊</button> <!-- Replace emoji with text or any other emoji if you want. -->
<script>
function speakText(text) {
var utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'en-US';
window.speechSynthesis.speak(utterance);
}
</script>
<?php
return ob_get_clean();
}
add_shortcode('tts', 'text_to_speech_shortcode');
// Shortcode usage example:- [tts value="Your text goes here"]
/* TTS END */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment