I need help creating Wordpress function to make shortcode [youtube-id=ID] in post editor output the following html code with ID value included:
<iframe width="420" height="315" src="https://www.youtube.com/embed/ID?rel=0" frameborder="0" allowfullscreen></iframe>
in your functions.php
function YouTubeID($atts, $content = null) {
extract(shortcode_atts(array('id'=>''), $atts));
return '<iframe width="420" height="315" src="https://www.youtube.com/embed/'.$id.'?rel=0" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode('youtube', 'YouTubeID');
wherever you want the video the shortcode will be:
[youtube id="THE_ID"]
Try this
function my_amazing_shortcode_handler($args)
{
$args=shortcode_atts(array('id' => 'some_default_id'), $args);
return '<iframe width="420" height="315" src="https://www.youtube.com/embed/' . $args['id'] . '?rel=0" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode('youtube_iframe', 'my_amazing_shortcode_handler');
Use like [youtube_iframe id='some_id']
For reference: add_shortcode, shortcode_atts
Related
I have a submit form for adding videos on my wordpress site that I create this code...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url = preg_replace($search,$replace,$url1);
?>
<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
This code will take youtube url and change it to embed code and add src to iframe, also i can post src source from embedable source and it will also work, but...
What I want to do now is if in field I put
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoid" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
take https://www.youtube.com/embed/videoidfrom code above and put it in $url.
When I try it with existing code, my output is...
<iframe width="560" height="315" src="<iframe width=" iframe-embed"="" scrolling="no" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
Update:
I found the way how embed codes will be extracted its via this function...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Now, I need to combine first function and seconds one, php code should check if its embed code that has SRC value to use preg match for src andf extract src, else it should use first function.
Something like check if SRC value exists, and than use...
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
otherwise...
$url4 = $url2;
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = '<iframe width="560" height="315" src="'.$url2.'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
$url4 = preg_match('~iframe.*src="([^"]*)"~', $url3, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
I'm trying to search JW Player embed codes like this one:
<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>
What I need to grab is the code right after players/ which in this embed would be just ewQYJ6zA.
Anyone have an idea how to go about this?
This will search for players/ followed by one or more letters and numbers.
<?php
$string = '<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>';
preg_match( '~/players/([[a-zA-Z\d]+)~', $string, $matches );
echo $matches[1];
?>
Output:
ewQYJ6zA
This regex in PHP should do the trick:
preg_match('/players\/(.+)-/im', $codetomatch, $codeyouwant);
Regex101
I want to get all YouTube iframe using regex and want to add specific tag to each record found.
For example <youtube-frame></youtube-frame> to iframe begeing and end.
Needed output:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
My Code
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
What I have tried?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
Try this:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>$0</youtube-frame>', $embed);
This uses preg_replace and a global regex to replace all YouTube IFrame tags (including their closing tags) with <youtube-frame>$0</youtube-frame> where $0 is the original string.
The regex could theoretically be simplified if you are totally sure about the format of your input, but I designed it to be robust enough to cope with other syntaxes such as src=http://example.com or src = "http://example.com" etc. which are accepted by browsers nowadays, and it only matches sources on *.youtube.com domains and not something like myyoutubesite.com.
How can I detect and replace a soundcloud url in a text with an iframe using PHP:
For example:
This:
https://soundcloud.com/s/eminem-ft-dr-dre-old-time-sake
Into this:
<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140068709&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>
function linkifySoundcloudURLs( $text )
{
$text = preg_replace('#https{0,1}:\/\/w{0,3}\.*soundcloud\.com\/([^< ]+)#ix',
'<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https://soundcloud.com/$1&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>',
$text);
return $text;
}
Works for me.
I'm not sure I understand you correctly, but something like this?
<?php
$url = "https://soundcloud.com/aathmigan/eminem-ft-dr-dre-old-time-sake";
?>
<iframe width="100%" height="166" scrolling="no" frameborder="no"
src="<?php echo $url; ?>"></iframe>
How can I get the video ID for youtube and vimeo videos embed code? A youtube video embed code looks like this:
<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>
where the ID in this is dbPmWbqAJPs.
A vimeo embed code looks like this:
<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
where the ID in this example is 62468031.
I don't want to replace those exact ids, but rather have a pregmatch/pregreplace that finds the id in any embed of vimeo and of youtube.
try this
vimeo
$string='<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match_all( '~http://player.vimeo.com/video/(.*?)\?~si',$string,$M);
echo ($M[1][0]);
youtube :
$string='<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>';
preg_match_all( '~http://www.youtube.com/embed/(.*?)\"~si',$string,$M);
echo ($M[1][0]);
Try below code for php, it works without pregmatch
i.e. :
http://vimeo.com/22222222
http://player.vimeo.com/video/22222222
http://vimeo.com/channels/staffpicks/22222222
http://vimeo.com/groups/groupsname/22222222
PHP Code :
$vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
$fetchVimeoIdArr = explode('/', $vimeoUrl);
$idCounter = count($fetchVimeoIdArr) - 1;
$vimeoId = $fetchVimeoIdArr[$idCounter];