1

I'm using the built in Wordpress audio player with my own skin, I needed to set the default volume to 100 so I put this into footer.php

<!--Change Wordpress Audio Player Default Volume-->
<script type="text/javascript">
jQuery(document).ready(function ($) {
    $("audio").mediaelementplayer({
        success: function (mediaElement, domObject) {
            mediaElement.setVolume(1.0);
        }
    });
});
</script>

Problem is I get the error Uncaught TypeError: $(...).mediaelementplayer is not a function on any page that doesn't have the [audio] shortcode because the wp-mediaelement.min.js and mediaelement-and-player.min.js JS files aren't loading on the pages without the shortcode, this in turn breaks other javascript on those pages.

What are my options here? Having the default volume at max is a must. Is my code written wrong?

3

I can think of two possible options - although I am not been able to test either right now.

Option 1

In theory the cleaner one, as you detect if the mediaelementplayer plugin has loaded itself in the jQuery namespace:

<!--Change Wordpress Audio Player Default Volume-->
<script type="text/javascript">
jQuery(document).ready(function ($) {
    if($.fn.mediaelementplayer) {
        $("audio").mediaelementplayer({
            success: function (mediaElement, domObject) {
                mediaElement.setVolume(1.0);
            }
        });
    }
});
</script>

Option 2

If Option 1 doesn't work, then this should; it tests the current jQuery object to see if it responds to the mediaelementplayer call:

<!--Change Wordpress Audio Player Default Volume-->
<script type="text/javascript">
jQuery(document).ready(function ($) {
    var audio_widgets = $("audio");
    if(audio_widgets.mediaelementplayer) {
        audio_widgets.mediaelementplayer({
            success: function (mediaElement, domObject) {
                mediaElement.setVolume(1.0);
            }
        });
    }
});
</script>

Hops this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.