WordPress Shortcodes, Part 2 – [ audio ]

WordPress

In the first WordPress shortcode blog post, we introduced readers to the concept of using shortcodes to improve functionality. Beginning with this post, we will briefly discuss six shortcodes built into WordPress. The first of the six is the [ audio ] shortcode.

The [ audio ] shortcode makes it possible for you to embed audio files on any page or post. The function was added to WordPress back in October 2013. Using the shortcode is a simple way to have music playing in the background or presented via a media player.

Without the shortcode, you would need to add each audio source you wanted to be attached to a page or post with a single line of code that directs a browser to the source URL. That’s a fine way to do things for just one audio file. But if you want to use multiple files, adding sources one at a time can be cumbersome.

The Basic Code

Using the [ audio ] shortcode is a two-step process. First, you have to establish your parameters by taking a look at the default code included in the ‘wp-includes/media.php’ file in your WordPress directory. More on that in a minute. Assuming you do not want to change any the defaults, you can simply leave that file alone.

The second step is to insert the actual shortcode into your page or post. Go into the editor and view your page or post in text mode by clicking the button at the top right of the editor screen. You will then see all of the embedded code within that page or post or, in the case of brand-new page or post, a blank window. Enter your shortcode wherever you want it.

A simple shortcode to play a single audio file would look something like:

[ audio src="audio-source.mp3" ]

*Remove the spaces after ‘[‘ and before the ‘]’.

The ‘audio’ tag establishes that you are using the [ audio ] shortcode. The ‘src’ parameter is what you use to tell web browsers how to find the audio file you want to be played. Our example uses an MP3, but you could use other formats as well. The code supports OGG, WAV, etc.

If you wanted to make it possible to use different formats to account for different browsers, you could enter a shortcode that looks something like:

[ audio mp3="source.mp3" ogg="source.ogg" wav="source.wav" ]

*Remove the spaces after ‘[‘ and before the ‘]’.

Bear in mind that this simple code relies on the audio files you want played already being located in your media library. If you want to use files located remotely, you have to provide a URL in the ‘source’ tag.

Shortcode Options

The [ audio ] shortcode allows for a number of different options that can be set by modifying the ‘wp-includes/media.php’ file. Each of these options must be entered as a string under the audio heading:

  • Loop – Loop a single media file; the default is off
  • Autoplay – Force autoplay as soon as the media file is loaded; the default is off
  • Preload – Defines how media files are loaded; options include none, auto, and metadata.

The WP documentation offers more detailed instructions about using these parameters.

Share it!Share on FacebookTweet about this on TwitterShare on Google+Email this to someone

Our Recommended Wordpress Web Hosting Provider

WordPress

WHC.CA
Get a 50% Discount by using our link.

WHC.ca

Reliable host, with excellent customer service and an easy one-click Wordpress installation. Our recommendation to host a Wordpress blog.

Leave a Reply

Your email address will not be published. Required fields are marked *