Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active October 30, 2022 06:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save srikat/8525732 to your computer and use it in GitHub Desktop.
Save srikat/8525732 to your computer and use it in GitHub Desktop.
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