Created
November 15, 2025 11:28
-
-
Save marklchaves/7a02ee1ed6f4a53b502803200aa96634 to your computer and use it in GitHub Desktop.
Stop (pause) and audio file when you close its Popup Maker popup.
This file contains hidden or 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
| <?php | |
| /** | |
| * Stop (pause) and audio file when you close its popup. | |
| */ | |
| function stop_audio_on_close() { ?> | |
| <script type="text/javascript"> | |
| jQuery(document).ready(function ($) { | |
| // Change this to your popup ID. | |
| const POPUP_ID = '#pum-855'; | |
| // 1. Get a reference to the audio element using a generic selector | |
| // If you have multiple audio elements, give your target one an ID (e.g., #myAudio) | |
| // and use: const $audioElement = $('#myAudio'); | |
| const $audioElement = $('audio'); | |
| // 2. Get a reference to the Popup Maker element (PUM) | |
| const $popupElement = $(POPUP_ID); | |
| // Check if the audio element was found | |
| if ($audioElement.length) { | |
| // --- Console Logging for Debugging --- | |
| $audioElement.on('play', function() { | |
| console.log('Audio STARTED playing.'); | |
| }); | |
| $audioElement.on('pause', function() { | |
| // Use the native DOM element to check if it ended | |
| if (this.ended) { | |
| console.log('Audio STOPPED: Playback FINISHED.'); | |
| } else { | |
| console.log('Audio PAUSED.'); | |
| } | |
| }); | |
| // --- Core Logic: Pause Audio on Popup Close --- | |
| if ($popupElement.length) { | |
| // Listen for the custom PUM event | |
| $popupElement.on('pumAfterClose', function() { | |
| // Get the native audio DOM element to call the .pause() method | |
| const audioDOM = $audioElement[0]; | |
| // Check if the audio is currently playing before pausing | |
| if (audioDOM && !audioDOM.paused) { | |
| audioDOM.pause(); | |
| console.log('Audio paused because ' + POPUP_ID + ' fired pumAfterClose.'); | |
| } | |
| }); | |
| console.log('Audio control listeners attached successfully.'); | |
| } else { | |
| console.error('Could not find the Popup Maker element ' + POPUP_ID + '. Please check the ID.'); | |
| } | |
| } else { | |
| console.error('Could not find an <audio> element on the page.'); | |
| } | |
| }); // jQuery | |
| </script> | |
| <?php } | |
| add_action( 'wp_footer', 'stop_audio_on_close', 500 ); // Load the script in the footer with a "late" priority. | |
| /** | |
| * You can add the PHP code snippet to your child theme's functions.php file | |
| * or with third-party plugins such as My Custom Functions and Code Snippets. | |
| * | |
| * Learn more: | |
| * - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-js/ | |
| * - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-php/ | |
| * - https://wppopupmaker.com/docs/manage-features-or-options/find-the-popup-id/ | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment