tainterr
tainterr

Reputation: 73

trigger shortcode with link of a certain class

I have a feed that I am ingesting directly into Wordpress 3.6 as posts. It gives all of the audio files as links with a certain class. I am using the built in audio that just requires the mp3 url to embed the player (or it can use the audio shortcode) . I'm curious to find out if there is a way other than hacking the ingest plugin (stripping everything but the url) when it parses the feed or using regex in functions.php to do the same, that could trigger the shortcode from a link of a certain class?

So this is what it looks like:

<a class="audio-asset" href="http://....

And I want it to be processed by WP as either 1. the url to embed as audio or 2. same result but be viewed as the shortcode

[audio mp3="http://...

Upvotes: 0

Views: 468

Answers (1)

manishie
manishie

Reputation: 5322

This is easy to do with a tiny bit of PHP and Javascript, since the WP 3.6 built-in audio and video shortcodes use mediaelement.js.

Let's say that your feed brings in audio elements such as:

<audio src="http://mediaelementjs.com/media/AirReview-Landmarks-02-ChasingCorporate.mp3" type="audio/mp3"></audio>

Normally if you had used an audio shortcode ([audio mp3="..."]), wordpress would automatically load the necessary javascript and css for the media player. In this case, you need to do that yourself. So for those pages, you'd want to add this PHP code to load the required files:

wp_enqueue_script('wp-mediaelement', array('jquery'));
wp_enqueue_style('wp-mediaelement');

Now add some jQuery to your page to initialize the media player for any audio elements on the page:

jQuery(function($) {
    $('audio').mediaelementplayer();
});

Put this all together and you have the same exact effect as if you had used the audio shortcode.

Upvotes: 0

Related Questions