How To Easily Embed SermonAudio Sermons In WordPress

SermonAudio.com is a powerful media browser with tons of great content. It allows ministries to easily put their sermons online for the world to listen. SermonAudio handles everything from audio to video streaming across a huge range of platforms.

Adding sermons to WordPress is a fairly simple task given WordPress’ oEmbed API. Churches, bloggers, and ministries could easily take advantage of it and should. Sermons can be embedded as citations, as recommendations, or even just to keep the content in circulation.

For the sake of this how-to I’ll be showing you the easiest way to embed SermonAudio sermons. I won’t go into the advanced process of creating widgets and shortcodes. I’ll only show you how to make sermonaudio.com links embed audio sermons automatically.

WordPress’ oEmbed API

WordPress has had the oEmbed API since version 2.9. It has consistently been updated, and has added support for new default providers over time. Unfortunately SermonAudio is not on their list of default providers, but there’s hope! The API itself is extendable so you are free to add your own providers if they’re not natively supported.

Using The API

oEmbed is a simple way to allow URLs to display embeddable content automatically. If you were to paste a link to a YouTube video in your post, WordPress would automatically get everything needed to display the video on your site.

Greg Bahnsen's Final Sermon

YouTube is only one example of a default provider, for a full list you can visit the WordPress codex.

Extend The API to Embed SermonAudio Sermons

Now lets try to achieve the same effect for SermonAudio content. In order for us to embed SermonAudio sermons we’re going to need to register SermonAudio as a provider, and then provide WordPress with link to SermonAudio’s sermon page.

We can register the provider by hooking into the init action in WordPress and adding the provider below.

Now that we have the provider registered we need to provide it with a url. This should be the url that we would like to be embeddable. On a side note, we will be working with some regex to parse the url. As an example we’ll use this sermon’s url.

http://www.sermonaudio.com/sermoninfo.asp?SID=36161418311

You may have already noticed the query variable “SID” in the URL. The value it holds is the ID of the sermon, and is what we need to parse so that we can render our sermon.

The first match in the regex is (www\.) which means that the www is optional. The second match in the regex is ([\d]+), this match requires that the value for SID is only an integer. Note, this will not work with a string.

Now let’s register SermonAudio as a valid provider, supply it with our regex string, and lastly give it a callback function to execute when regex finds a match.

Write The Callback Function

Now we need to add the proper callback wp_register_sermonaudio_embed to create the html needed for the embed.

Let’s try to understand what we’re working with. Our callback by default has 4 variables passed into it. The first variable is an array of regex matches, and for the sake of this tutorial we’ll only deal with that variable. Now let’s use the matches.

We use a sprintf function to pass the data into our iframe. The iframe makes a call to SermonAudio’s web player with the provided sermon ID which is stored in the second key of the $matches array.

Lastly we’ll return the $embed variable as a filter.

And that’s it! Now you can easily embed SermonAudio sermons by pasting SermonAudio links into your post, which would result in the following.

after embed SermonAudio sermons

The Plugin

In case you were looking for a plugin to do this job, I went ahead and packaged all of this plus a little bit more into a plugin which will be on the WordPress Plugin Repo soon. You can grab it now on Github. The plugin allows for multiple urls to be embeddable and currently only supports audio embeds. You can download them below.

Download on Github

Conclusion

SermonAudio is a really great platform that you can utilize for your ministry. In fact, I’ve built a few tools that integrate with their services already. If you’re already using them to power your online ministry I would highly recommend working with their APIs to customize your user’s experience.

Connecting to SermonAudio.com Via PHP

Displaying your content from sermonaudio.com just got easier. I first read about their JSON API about a year ago, and naturally I was pretty excited. I’ve personally benefited a ton from the preaching of men such as Joe Morecraft, Bojidar Marinov, Todd Ruddell, Jeff Noblit, Steven Lawson, etc, and I thought it was only fitting that I create a way to give back. At different times over the past year or so I’ve worked on an API for PHP that allows you to do just that, you can view an example of a small app that I built in Angularjs and PHP here. I’ll explain how to use it in depth below.

Setup and Installation

First thing is first, your church must be registered as a full member on sermonaudio.com as in order to attain an API key. If you already use SA to publish your church’s sermons then this shouldn’t be a problem, otherwise you can register here.

Once you’ve created an account, you’ll need to setup an API key in order to have access to your church’s sermons, speakers, events, and languages; you can do that here.

Now that you’ve got your API key, all you need to do is download my repo from github that will connect you to the platform.

https://github.com/CarlosRios/sermonaudio-php-api

After you’ve downloaded or cloned the repo you can then install it by including it in your project

Making the connection

Now that you have the API included in your project you can begin setting it up. First setup your API Key using the setApiKey() method.

You can test your API key to see if it’s accepted by running the getSermons() method which should return an array of sermon objects if the key was accepted, and false if it was rejected.

Using the PHP API

Handling the sermons

Once you’ve verified that your key is accepted you can begin by grabbing sermons like we did above. The getSermons() method is a helper method included in the SermonAudioApi class that will build the api request with the corresponding options, and then make the request. The getSermons() method accepts an array of options.

speaker: the name of the speaker exactly as it is on SermonAudio
event: a specific type of event that is associated with sermons, ex ( Sunday Service ).
series: the name of the sermon series as it is on SermonAudio
page: the page number to retrieve the sermons from. Set to “total” to retrieve all sermons
sermons_per_page: the number of sermons to display per page, max is currently 100
year: narrows the sermons down to a specific year

Using the sermons after you’ve made the api request is as easy as running a loop. Each sermon will return with the fields provided by sermon audio in a standard php object.

Handling the Speakers

SermonAudio allows us to grab all of the speakers listed in your SermonAudio account. To do so all we need to do is make the proper request, and SA will send us a list of the speakers.

After we have a list of the speakers, we can loop through them as well.

Handling the Events

Events are really short for event types. SA allows you to group your sermons by event type in their dashboard which allows you to distinguish between something like a 9:00am service and an 11:00am service. To access the types of events you’ve registered with SA, all we need to do is make the proper request.

Handling the Languages

Lastly, to get the languages you’ve registered with SA, all we use the getLanguages() method

Using your own methods to make server requests

By default the PHP API for SermonAudio comes with a nifty requestData() method which is used to make all of the server requests to SA. The requests are made using the file_get_contents function provided with PHP, which happens to work on almost all server configurations. After the requests are made and the JSON is collected, it is then converted into standard PHP objects which can then be used in your application.

Not satisfied with file_get_contents? Well there’s no need to worry, the API has also been prepared in case you would rather handle the requests and parsing of the data yourself. All of the get methods have a route method that they utilize to build the request url before the request is actually made. You can use any of the methods below to get the url where you can make the request yourself and handle the data as you please.

sermonsApiRoute( $args );
Builds the url to grab the sermons, and uses all of the same arguments used in the getSermons method.

speakersApiRoute();
Builds the url to grab the speakers.

eventsApiRoute();
Builds the url to grab all of the event types.

languagesApiRoute();
Builds the url to grab all of the languages.

totalSermonsApiRoute( $args );
Builds the url to grab the total number of sermons. Can use any of the arguments available in the getSermons method except for “pages” and “sermons_per_page”.