Audio Player
-
Is there a way to display the audio player in the mla_gallery?
[mla_gallery post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="File: {+file_url+}<br>Caption: {+caption+}<br>Description: {+description+}<br>Excerpt: {+excerpt+}" link=file columns=1 posts_per_page=7] [mla_gallery post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="File: {+file_url+}<br>Caption: {+caption+}<br>Description: {+description+}<br>Excerpt: {+excerpt+}" link=file columns=1 posts_per_page=7 mla_output="paginate_links,prev_next"]
-
Thanks for an interesting question. Thanks as well for including the source text of your shortcodes; that’s very helpful.
WordPress provides an “Audio Shortcode“, but it works somewhat differently from the
[gallery]
and[mla_gallery]
shortcodes. The[audio]
shortcode does not use the attachment ID values to specify which file to play; it requires the URL of the audio file.The easiest way to get what you want is to use a small custom plugin that hooks a couple of the filters provided by
[mla_gallery]
. The example plugin below looks for amy_custom_audio
parameter in your[mla_gallery]
shortcode. If this parameter is present, the thumbnail/title link for each gallery item is replaced by the “audio player”. You can add the autoplay, loop and preload arguments to themy_custom_audio
parameter if you want to pass them on to the[audio]
shortcode.To use the new plugin you need to add a parameter to the first of your two shortcodes. The second shortcode with pagination controls does not need the new parameter. Unless you need to specify loop or preload values, you can try this:
[mla_gallery my_custom_audio="" post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="File: {+file_url+}<br>Caption: {+caption+}<br>Description: {+description+}<br>Excerpt: {+excerpt+}" link=file columns=1 posts_per_page=7]
I have included the complete source code for the plugin below. You can copy it into a file named mla-audio-shortcode-example.php and put the file in your /wp-content/plugins directory. Then, go to your wp-admin “Plugins/Installed Plugins” screen and activate the “MLA Audio Shortcode Example” plugin.
If you would like a copy by e-mail, you can send me your contact information from the Contact Us page at our web site:
If you need more specific guidance on installing the code as a plugin on your system, let me know. I will leave this topic unresolved until I hear back from you. Thanks for an interesting question and for your interest in the plugin.
PLUGIN SOURCE CODE
<?php /** * Provides an example of hooking the filters provided by the [mla_gallery] shortcode: * * This example replaces the gallery item content with "audio player" elements generated * by the WordPress [audio] shortcode. * * @package MLA Audio Shortcode Example * @version 1.00 */ /* Plugin Name: MLA Audio Shortcode Example Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Provides an example of hooking the filters provided by the [mla_gallery] shortcode Author: David Lingren Version: 1.00 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2013, 2014 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA Audio Shortcode Example hooks two of the filters provided by the [mla_gallery] shortcode * * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding enerything * else inside a class means this is the only name you have to worry about. * * @package MLA Audio Shortcode Example * @since 1.00 */ class MLAAudioShortcodeExample { /** * Initialization function, similar to __construct() * * @since 1.00 * * @return void */ public static function initialize() { /* * The filters are only useful for front-end posts/pages; exit if in the admin section */ if ( is_admin() ) return; /* * add_filter parameters: * $tag - name of the hook you're filtering; defined by [mla_gallery] * $function_to_add - function to be called when [mla_gallery] applies the filter * $priority - default 10; lower runs earlier, higher runs later * $accepted_args - number of arguments your function accepts */ add_filter( 'mla_gallery_attributes', 'MLAAudioShortcodeExample::mla_gallery_attributes_filter', 10, 1 ); add_filter( 'mla_gallery_item_values', 'MLAAudioShortcodeExample::mla_gallery_item_values_filter', 10, 1 ); } /** * Save the shortcode attributes * * @since 1.00 * * @var array */ private static $shortcode_attributes = array(); /** * MLA Gallery (Display) Attributes * * This filter gives you an opportunity to record or modify the arguments passed in to the shortcode * before they are merged with the default arguments used for the gallery display. * * The $shortcode_attributes array is where you will find any of your own parameters that are coded in the * shortcode, e.g., [mla_gallery my_parameter="my value"]. * * @since 1.00 * * @param array the shortcode parameters passed in to the shortcode * * @return array updated shortcode attributes */ public static function mla_gallery_attributes_filter( $shortcode_attributes ) { /* * Save the attributes for use in the later filters */ self::$shortcode_attributes = $shortcode_attributes; return $shortcode_attributes; } // mla_gallery_attributes_filter /** * MLA Gallery Item Values * * @since 1.00 * * @param array parameter_name => parameter_value pairs * * @return array updated substitution parameter name => value pairs */ public static function mla_gallery_item_values_filter( $item_values ) { /* * We use a shortcode parameter of our own to apply this filter on a gallery-by-gallery * basis, leaving other [mla_gallery] instances untouched. If the "my_custom_audio" * parameter is not present, we have nothing to do. If the parameter IS present, * we replace the [+link+] value with the [audio] shortcode output. * * The "my_custom_audio" parameter can be used to pass parameters to the [audio] shortcode, * such as autoplay, loop and preload. No validation of the parameters is done here. */ if ( isset( self::$shortcode_attributes['my_custom_audio'] ) ) { $audio_args = self::$shortcode_attributes['my_custom_audio']; if ( empty( $audio_args ) ) { $audio_args = array(); } elseif ( is_string( $audio_args ) ) { $audio_args = shortcode_parse_atts( $audio_args ); } $audio_args['src'] = $item_values['base_url'] . '/' . $item_values['base_file']; $item_values['link'] = wp_audio_shortcode( $audio_args ); } return $item_values; } // mla_gallery_item_values_filter } // Class MLAAudioShortcodeExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAAudioShortcodeExample::initialize'); ?>
Perfect!!! Works like a charm. Thanks so much!
Here is the full code – just in case anyone else wants to do this.
<form id="attachment-category-form" method="get" action="/audiotest/"> <select name="category_value" class="postform"> <option value="">-- All Topics --</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <<option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> </select> <input type="text" id="mla-search-box" name="search-string" value="" placeholder="Keyword Search"> <input type="submit" id="submit" name="submit" value="GO"> </form> [mla_gallery my_custom_audio="" post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="<strong><br />Title:</strong> {+title+}<strong><br />Scripture:</strong> {+caption+}<br /><strong>Description: </strong>{+description+}<br /><strong>Excerpt:</strong> {+excerpt+}<br /><strong>Topic: </strong>{+terms:attachment_category+}<br /><br /><hr align=center noshade width=25%>" link=file mla_target="_blank" columns=1 posts_per_page=10] [mla_gallery post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="<strong><br />Title:</strong> {+title+}<strong><br />Scripture:</strong> {+caption+}<br /><strong>Description:</strong> {+description+}<br /><strong>Excerpt:</strong> {+excerpt+}<br /><strong>Topic: </strong>{+terms:attachment_category+}<br /><br /><hr align=center noshade width=25%>" link=file columns=1 posts_per_page=10 mla_output="paginate_links,prev_next"]
So sorry… had an extra < in the code. Here is the corrected code
<form id="attachment-category-form" method="get" action="/audiotest/"> <select name="category_value" class="postform"> <option value="">-- All Topics --</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> <option value="xyz">XYZ</option> </select> <input type="text" id="mla-search-box" name="search-string" value="" placeholder="Keyword Search"> <input type="submit" id="submit" name="submit" value="GO"> </form> [mla_gallery my_custom_audio="" post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="<strong><br />Title:</strong> {+title+}<strong><br />Scripture:</strong> {+caption+}<br /><strong>Description: </strong>{+description+}<br /><strong>Excerpt:</strong> {+excerpt+}<br /><strong>Topic: </strong>{+terms:attachment_category+}<br /><br /><hr align=center noshade width=25%>" link=file mla_target="_blank" columns=1 posts_per_page=10] [mla_gallery post_mime_type="audio/mpeg" s="{+request:search-string+}" attachment_category="{+request:category_value+}" mla_caption="<strong><br />Title:</strong> {+title+}<strong><br />Scripture:</strong> {+caption+}<br /><strong>Description:</strong> {+description+}<br /><strong>Excerpt:</strong> {+excerpt+}<br /><strong>Topic: </strong>{+terms:attachment_category+}<br /><br /><hr align=center noshade width=25%>" link=file columns=1 posts_per_page=10 mla_output="paginate_links,prev_next"]
Thank you for your updates and for the good news. I am happy to hear the custom plugin worked for you, and I will add it to the
/examples
directory in my next version.Thanks as well for taking the time to post your code here so other users can benefit!
I am marking this topic resolved, but please let me know if there is anything else I can do to help. Thanks for your interest in the plugin.
- The topic ‘Audio Player’ is closed to new replies.