Reputation: 169
I have a rather annoying problem: I have a wordpress website that, among other things, serves audio files. Wordpress does a nice job of filtering any media inserts to a post and putting a player in, but this isn't much good for allowing downloads of the file as well. Ideally I'd want to change the code that generates the audio player shortcode and add <a href="URL">Click here to download mp3.</a>
after the [/audio]
tag, but my PHP is pretty much non-existent.
My current approach is to use python (which I know well) to write an hourly cron script which plays with the mysql database directly using the python mysql connector. If it runs hourly and looks for posts made in the last hour which contain the string [/audio]
, pulls out the url for the audio file, and uses it to construct a link to append afterwards. The effect of this is the same, with a maximum time delay of one hour between the post being made and the link being available. When I've finished coding it it should work, but I'm sure it's not the best approach.
Any suggestions?
Upvotes: 2
Views: 828
Reputation: 198
This should do it (add this to your functions.php file)
function wp_audio_shortcode_dowload_link( $html, $atts, $audio, $post_id, $library ) {
$html .='<br><a href="'.$atts['src'].'">Download</a>';
return $html;
}
add_filter( 'wp_audio_shortcode','wp_audio_shortcode_dowload_link',5,10);
Upvotes: 1
Reputation: 254212
In this snippet you have the original code for [audio] shortcode.
It is located in wp-includes/media.php
and begins at line 2171. The code in this snippet:
2171 function wp_audio_shortcode( $attr, $content = '' ) {
2172 $post_id = get_post() ? get_the_ID() : 0;
2173
2174 static $instance = 0;
2175 $instance++;
2176
2177 /**
2178 * Filter the default audio shortcode output.
2179 *
2180 * If the filtered output isn't empty, it will be used instead of generating the default audio template.
2181 *
2182 * @since 3.6.0
2183 *
2184 * @param string $html Empty variable to be replaced with shortcode markup.
2185 * @param array $attr Attributes of the shortcode. @see wp_audio_shortcode()
2186 * @param string $content Shortcode content.
2187 * @param int $instance Unique numeric ID of this audio shortcode instance.
2188 */
2189 $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );
2190 if ( '' !== $override ) {
2191 return $override;
2192 }
2193
2194 $audio = null;
2195
2196 $default_types = wp_get_audio_extensions();
2197 $defaults_atts = array(
2198 'src' => '',
2199 'loop' => '',
2200 'autoplay' => '',
2201 'preload' => 'none',
2202 'class' => 'wp-audio-shortcode',
2203 'style' => 'width: 100%; visibility: hidden;'
2204 );
2205 foreach ( $default_types as $type ) {
2206 $defaults_atts[$type] = '';
2207 }
2208
2209 $atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
2210
2211 $primary = false;
2212 if ( ! empty( $atts['src'] ) ) {
2213 $type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
2214 if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
2215 return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
2216 }
2217 $primary = true;
2218 array_unshift( $default_types, 'src' );
2219 } else {
2220 foreach ( $default_types as $ext ) {
2221 if ( ! empty( $atts[ $ext ] ) ) {
2222 $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
2223 if ( strtolower( $type['ext'] ) === $ext ) {
2224 $primary = true;
2225 }
2226 }
2227 }
2228 }
2229
2230 if ( ! $primary ) {
2231 $audios = get_attached_media( 'audio', $post_id );
2232 if ( empty( $audios ) ) {
2233 return;
2234 }
2235
2236 $audio = reset( $audios );
2237 $atts['src'] = wp_get_attachment_url( $audio->ID );
2238 if ( empty( $atts['src'] ) ) {
2239 return;
2240 }
2241
2242 array_unshift( $default_types, 'src' );
2243 }
2244
2245 /**
2246 * Filter the media library used for the audio shortcode.
2247 *
2248 * @since 3.6.0
2249 *
2250 * @param string $library Media library used for the audio shortcode.
2251 */
2252 $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
2253 if ( 'mediaelement' === $library && did_action( 'init' ) ) {
2254 wp_enqueue_style( 'wp-mediaelement' );
2255 wp_enqueue_script( 'wp-mediaelement' );
2256 }
2257
2258 /**
2259 * Filter the class attribute for the audio shortcode output container.
2260 *
2261 * @since 3.6.0
2262 *
2263 * @param string $class CSS class or list of space-separated classes.
2264 */
2265 $atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] );
2266
2267 $html_atts = array(
2268 'class' => $atts['class'],
2269 'id' => sprintf( 'audio-%d-%d', $post_id, $instance ),
2270 'loop' => wp_validate_boolean( $atts['loop'] ),
2271 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
2272 'preload' => $atts['preload'],
2273 'style' => $atts['style'],
2274 );
2275
2276 // These ones should just be omitted altogether if they are blank
2277 foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
2278 if ( empty( $html_atts[$a] ) ) {
2279 unset( $html_atts[$a] );
2280 }
2281 }
2282
2283 $attr_strings = array();
2284 foreach ( $html_atts as $k => $v ) {
2285 $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
2286 }
2287
2288 $html = '';
2289 if ( 'mediaelement' === $library && 1 === $instance ) {
2290 $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
2291 }
2292 $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
2293
2294 $fileurl = '';
2295 $source = '<source type="%s" src="%s" />';
2296 foreach ( $default_types as $fallback ) {
2297 if ( ! empty( $atts[ $fallback ] ) ) {
2298 if ( empty( $fileurl ) ) {
2299 $fileurl = $atts[ $fallback ];
2300 }
2301 $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
2302 $url = add_query_arg( '_', $instance, $atts[ $fallback ] );
2303 $html .= sprintf( $source, $type['type'], esc_url( $url ) );
2304 }
2305 }
2306
2307 if ( 'mediaelement' === $library ) {
2308 $html .= wp_mediaelement_fallback( $fileurl );
2309 }
2310 $html .= '</audio>';
2311
2312 /**
2313 * Filter the audio shortcode output.
2314 *
2315 * @since 3.6.0
2316 *
2317 * @param string $html Audio shortcode HTML output.
2318 * @param array $atts Array of audio shortcode attributes.
2319 * @param string $audio Audio file.
2320 * @param int $post_id Post ID.
2321 * @param string $library Media library used for the audio shortcode.
2322 */
2323 return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
2324 }
2325 add_shortcode( 'audio', 'wp_audio_shortcode' );
The idea is to create your own custom version [audio] shortcode, copying and customizing the original code in the function.php
file of your active child theme or theme.
You should use first remove_shortcode( 'audio' );
before registering your custom [audio]
shortcode.
Upvotes: 1