Last active
December 25, 2019 13:06
-
-
Save johnregan3/77400c2bc335387ad38a1abfb45c9b07 to your computer and use it in GitHub Desktop.
Filtering WP Shortcodes to comply with AMP standards.
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 | |
/** | |
* Hijack a hypothetical [podcast] custom shortcode for AMP. | |
* | |
* Replaces [podcast] output with amp-audio. | |
* | |
* Be sure you've added the amp-audio component script to the template. | |
* | |
* @filter the_content | |
* | |
* @param string $content The Post content. | |
* | |
* @return string | |
*/ | |
function jr3_filter_shortcode_podcast( $content ) { | |
global $shortcode_tags; | |
if ( ! is_amp_endpoint() || empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { | |
return $content; | |
} | |
$regex = get_shortcode_regex( array( 'podcast' ) ); | |
if ( ! $url = jr3_get_shortcode_attr( $content, $regex, 'mp3' ) ) { | |
// Strip out the shortcode to prevent errant output. | |
return preg_replace( "/$regex/", '', $content ); | |
} | |
// AMP's amp-audio only supports https:// and //, so ensure we're not using http://. | |
$url = str_replace( 'http:', '', $url ); | |
$url = trim( $url ); | |
ob_start(); | |
?> | |
<amp-audio width="auto" | |
height="50" | |
src="<?php echo esc_url( $url ); ?>"> | |
<div fallback> | |
<p><?php esc_html_e( 'Your browser doesn\'t support HTML5 audio', 'wp-amp-tutorial' ); ?></p> | |
</div> | |
</amp-audio> | |
<?php | |
$amp_audio = ob_get_clean(); | |
$content = preg_replace( "/$regex/", $amp_audio, $content ); | |
return $content; | |
} | |
add_filter( 'the_content', 'jr3_filter_shortcode_podcast' ); | |
/** | |
* Get a value from a shortcode. | |
* | |
* Helper function for xwp_filter_shortcode_audio(). | |
* | |
* Example: | |
* | |
* Search for the audio shortcode looks like this in the Post content: | |
* [podcast url="http://example.com/audio.mp3"] | |
* and you want to get the value of "url"... | |
* | |
* Do this: | |
* $regex = get_shortcode_regex( 'podcast' ); | |
* $url_value = xwp_get_shortcode_attr( $content, $regex, 'url' ); | |
* | |
* The $url_value will now be "http://example.com/audio.mp3". | |
* | |
* @param string $content Post content. | |
* @param string $regex Regex to compare. | |
* @param string $search The Shortcode param to find. | |
* | |
* @return mixed Param content, else false. | |
*/ | |
function jr3_get_shortcode_attr( $content, $regex, $search ) { | |
// Find the shortcode. | |
preg_match( "/$regex/", $content, $shortcode_atts ); | |
$search = $search . '='; | |
if ( empty( $shortcode_atts ) ) { | |
return false; | |
} | |
// Find item in atts array that starts with $search. | |
$matches = array_filter( $shortcode_atts, function( $var ) use ( $search ) { | |
return ( 0 === strpos( trim( $var ), $search ) ); | |
} | |
); | |
if ( empty( $matches ) ) { | |
return false; | |
} | |
// Get the first occurrence of our string. | |
$value_string = array_shift( array_values( $matches ) ); | |
$val = str_replace( $search , '', $value_string ); | |
// Strip out both double and possible single quotes. | |
$val = str_replace( '"', '', $val ); | |
$val = str_replace( "'", '', $val ); | |
return trim( $val ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment