Last active
May 4, 2017 13:48
-
-
Save smutek/f11531a44327761c9cb7b660a9a78b95 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* oEmbed Attributes | |
* | |
* Add parameters to oEmbed query string. Useful for | |
* turning off related videos and such. | |
* | |
* Basic field use: $video = videoLink('your_field_name'); | |
* Add second param if in a repeater: $video - videoLink('your_subfield_name', true); | |
* | |
* @see https://www.advancedcustomfields.com/resources/oembed/ | |
* | |
* @param $field | |
* @param bool $repeater defaults to false / true if repeater | |
* @param array $params / array of query string parameters as key value pairs | |
* | |
* @return mixed embed HTML | |
*/ | |
function videoLink( $field, $repeater = false, array $params = [] ) { | |
global $post; | |
// get current post ID | |
$id = $post->ID; | |
// set of default params | |
$defaults = array( | |
'rel' => 0, | |
'title' => 0, | |
'byline' => 0, | |
'portrait' => 0, | |
'autoplay' => 'true' | |
); | |
// Check if defaults have been passed in via function args, | |
// if not then add them to params array | |
foreach ( $defaults as $key => $value ) { | |
if(!array_key_exists($key, $params)) { | |
$params[$key] = $value; | |
} | |
} | |
if ( ! $repeater ) { | |
// get the field | |
$videoFrame = get_field( $field, $id ); | |
} else { | |
// if we are in a repeater | |
$videoFrame = get_sub_field( $field, $id ); | |
} | |
// use preg_match to find iframe src | |
preg_match( '/src="(.+?)"/', $videoFrame, $matches ); | |
$src = $matches[1]; | |
// add extra params to iframe src | |
$new_src = add_query_arg( $params, $src ); | |
$videoLink = str_replace( $src, $new_src, $videoFrame ); | |
return $videoLink; | |
} |
Alex, I'm just seeing this. That's a good find, I haven't tried this out but are you suggesting something like this -
function videoLink($field) {
global $post;
// get current post ID
$id = $post->ID;
// is this a repeater?
$repeater = acf_get_loop('active');
if(!$repeater) {
// get the field
$videoFrame = get_field( $field, $id );
} else {
// if we are in a repeater
$videoFrame = get_sub_field( $field, $id );
}
// ... etc...
That would get rid of having to pass the class second param, but I feel like it would be less efficient because now it's running acf_get_loop('active')
twice - once in this function, and then again when it returns true and get_sub_field()
runs.
What do you think? Am I missing something?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get_sub_field()
appears to check if it is in a loop before it runs. I'm sure you could put the same logic in this. Here's the critical part ofget_sub_field()
that I think can be duplicated in this:Also, this is really useful!