Last active
April 12, 2022 13:02
-
-
Save damiencarbery/8e7cf08db9894ce77b5bbf9fa56d8a44 to your computer and use it in GitHub Desktop.
Append a Download link to the WordPress audio shortcode output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Append Download button below audio player | |
Plugin URI: https://www.damiencarbery.com/2017/02/add-download-link-to-wordpress-audio-player/ | |
GitHub Plugin URI: https://github.com/damiencarbery/audio-shortcode-download-link | |
Description: Add a Download link to the html generated for the "audio" shortcode. | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
add_filter( 'wp_audio_shortcode', 'ast_audio_shortcode_enhancer', 10, 5 ); | |
function ast_audio_shortcode_enhancer( $html, $atts, $audio, $post_id, $library ) { | |
/*error_log( 'HTML: ' . $html ); | |
error_log( 'ATTS: ' . var_export( $atts, true ) ); | |
error_log( 'AUDIO: ' . var_export( $audio, true ) ); | |
error_log( 'POST_ID: ' . var_export( $post_id, true ) ); | |
error_log( 'LIBRARY: ' . var_export( $library, true ) );*/ | |
$audio_types = array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ); | |
// Use the first audio type that has data. | |
foreach ( $audio_types as $extension ) { | |
if ( array_key_exists( $extension, $atts ) && strlen( $atts[ $extension ] ) ) { | |
return $html . sprintf( '<p><button type="button"><a href="%s" download>Download</a></button></p>', $atts[ $extension ] ); | |
break; | |
} | |
} | |
// For an externally hosted file the [embed] shortcode is active and the url will be in $atts['src']. | |
if ( array_key_exists( 'src', $atts ) ) { | |
return $html . sprintf( '<p><button type="button"><a href="%s" download>Download</a></button></p>', $atts[ 'src' ] ); | |
} | |
// Otherwise return the original html. | |
return $html; | |
} | |
// Add a Download button below the Audio block. | |
add_filter( 'render_block', 'dcwd_add_download_button_to_audio', 10, 2 ); | |
function dcwd_add_download_button_to_audio( $block_content, $block ) { | |
if ( 'core/audio' == $block['blockName'] ) { | |
if ( preg_match( '/src="(.*)"/', $block['innerHTML'], $matches ) ) { | |
return $block['innerHTML'] . sprintf( '<p><button type="button"><a href="%s" download>Download</a></button></p>', $matches[1] ); | |
} | |
} | |
return $block_content; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html:'<!--[if lt IE 9]><script>document.createElement(\'audio\');</script><![endif]--> | |
<audio class="wp-audio-shortcode" id="audio-1492-1" preload="none" style="width: 100%;" controls="controls"><source type="audio/mpeg" src="http://example.com/wp-content/uploads/2017/02/audio-track.mp3?_=1" /><a href="http://example.com/wp-content/uploads/2017/02/audio-track.mp3">http://example.com/wp-content/uploads/2017/02/audio-track.mp3</a></audio>' | |
atts:array ( | |
'src' => '', | |
'loop' => '', | |
'autoplay' => '', | |
'preload' => 'none', | |
'class' => 'wp-audio-shortcode', | |
'style' => 'width: 100%;', | |
'mp3' => 'http://example.com/wp-content/uploads/2017/02/audio-track.mp3', | |
'ogg' => '', | |
'wma' => '', | |
'm4a' => '', | |
'wav' => '', | |
) | |
audio:NULL | |
post_id:1492 | |
library:'mediaelement' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'wp_audio_shortcode', 'ast_audio_shortcode_filter_test', 10, 5 ); | |
function ast_audio_shortcode_filter_test( $html, $atts, $audio, $post_id, $library ) { | |
error_log( "html:".var_export( $html, true )."\natts:".var_export( $atts, true )."\naudio:".var_export( $audio, true )."\npost_id:".var_export( $post_id, true )."\nlibrary:".var_export( $library, true )."\n" ); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the combined effort peoples. Worked for me. At least until some major release of WordPress changes things again. And just how secure is it against damned invaders? No idea what to do with Shortcode filters. I liked the CSS of @meghansmith but I have no clue how to implement those filters she posted on her profile page. But @zizoua5000 gave me enough to work with. Thank you.