-
-
Save dexit/4cc096ca30d68f1a55a556f6d83c0933 to your computer and use it in GitHub Desktop.
Text to speech anywhere in wordpress
This file contains 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
/* 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