Instantly share code, notes, and snippets.

Embed
What would you like to do?
Playing a sound on hover/click of an element in WordPress. http://sridharkatakam.com/play-sound-hoverclick-element-wordpress/
<?php
//* Do NOT include the opening php tag
//* Enqueue audio script
add_action( 'wp_enqueue_scripts', 'enqueue_audio_on_menu_items_click' );
function enqueue_audio_on_menu_items_click() {
wp_enqueue_script( 'load-audio', get_stylesheet_directory_uri() . '/js/load-audio.js', array( 'jquery' ), '', true );
}
jQuery(document).ready(function($) {
// Adds a source element, and appends it to the audio element, represented by elem.
function addSource(elem, path) {
$('<source>').attr('src', path).appendTo(elem);
}
$(".site-header .genesis-nav-menu a").click(function(){
// Create an audio element, and set it to autoplay
var audio = $('<audio />', {
autoPlay : 'autoplay'
});
// Call our addSource function, and pass in the audio element and the path(s) to your audio.
addSource(audio, 'http://example.com/sounds/bell.ogg');
addSource(audio, 'http://example.com/sounds/bell.mp3');
// Add the audio + source elements to the page.
audio.appendTo('body');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment