I know i am missing something simple. I just want to display this iframe if $video-code exists. Can anyone see what is wrong with this? working in wordpress. error is on the echo line. i've also tried adding .'$video-code'. into the url.
it is displaying the iframe correctly, but the variable is displaying as text in the url. if i call the variable elsewhere in the page without the If statement, it displays correctly.
THANKS for any help!
<?php
$key = 'video-code';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<iframe id="player" width="560" height="315" frameborder="2" src="http://www.youtube.com/embed/$video-code" ></iframe>';
}?>
You can concatenate your $key, like so:
echo '<iframe id="player" width="560" height="315" frameborder="2"
src="http://www.youtube.com/embed/' . $key . '" ></iframe>';
Related
I have a variable as follows:
$post= 'TO UPLOAD DO THIS <img src="Christmas.PNG"> and this <iframe src="https://www.youtube.com/embed/aAkMkVFwAoo"></iframe>';
With the <img> tag I want to process the image to display diferently. I also want the video to be processed differently (customisable). I have done the following:
//Image processing:
preg_match_all('/<img src=\"(.*?)\">/',$post,$matches);
if(sizeof($matches[1])>0){
$toRmove=[];
$toAdd = [];
foreach($matches[1] as $m){
$value = $m;
$toRemove[]='<img src="'.$value.'">';
$toAdd[]='<img src="images/'.$value.'" width=20% height=20%>';
}
$new_message = str_replace($toRemove,$toAdd,$post);
$post= $new_message;
}
//video processing
preg_match_all('/<iframe src=\"(.*?)\">/',$post,$matches);
if(sizeof($matches[1])>0){
$toRmove=[];
$toAdd = [];
foreach($matches[1] as $m){
$value = $m;
$toRemove[] ='<iframe src="'.$value.'">';
$toAdd[] ='<iframe width="560" height="315" src="https://www.youtube.com/embed/aAkMkVFwAoo" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>';
}
$new_message = str_replace($toRemove,$toAdd,$post);
$post= $new_message;
}
echo $post;
When I echo $toAdd[0], the video displays correctly with what i want. However, after the str_replace($toRemove,$toAdd,$post); and then i echo $post the video just disappears but the image is still there and displaying correctly.
So the video processing is working but when it i go to add it, the video doesn't appear just blank.
Why is this occurring? Thanks.
How to make youtube video work on site without embeded code? I mean direct code as: https://www.youtube.com/watch?v=ouDmKW1FGjo. I want the user to paste direct video url into a field VIDEO_LINK not the embeded code. How can I do that? Only embeded link works on the below code not the direct link.
<iframe width="100%" height="550" src="<?php the_field('video_link') ?>" frameborder="0" allowfullscreen></iframe>
Use the following code to get video key (assuming the URL is https://www.youtube.com/watch?v=ouDmKW1FGjo):
<?php
$video_url=get_field('video_link');
$url = urldecode(rawurldecode($video_url));
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
// Get key of youtube by preg_match and put it in iframe
$videoKey= $matches[1];
?>
<iframe width="100%" height="550" src="https://www.youtube.com/embed/<?php echo $videoKey ;?>" frameborder="0" allowfullscreen></iframe>
You may use this function to convert Youtube URL to embeded code:
function get_youtube_embed($youtube_url, $width=560, $height=315)
{
$height = (int)$height;
$width = (int)$width;
$embed_html = '';
$parts = parse_url($youtube_url);
if(isset($parts['query'])) {
parse_str($parts['query'], $query);
if(isset($query['v'])) {
$embed_html = '<iframe width="'.$width.'" height="'.$height.'" src="https://www.youtube.com/embed/'.$query['v'].'" frameborder="0" allowfullscreen></iframe>';
}
}
return $embed_html;
}
i am trying to insert a link in an echoed line,
regular links work, but not this one, cant figure it out what's wrong
if($gallery_images != ''){
foreach ($gallery_images as $gallery_image){
$thumb = wp_get_attachment_image_src($gallery_image[SN.'gallery_post_image']['id'], 'post-thumb', false);
echo '<li><a <img src="'.$thumb[0].'" alt="'.$gallery_image[SN.'gallery_post_title'].'" /><p class="flex-caption">'.$gallery_image[SN.'gallery_post_title'].'</p></li>';
}
}
the_permalink() is a not a return function, it echoes the permalink. Replace it with get_permalink, which returns the permalink.
if($gallery_images != ''){
foreach ($gallery_images as $gallery_image){
$thumb = wp_get_attachment_image_src($gallery_image[SN.'gallery_post_image']['id'], 'post-thumb', false);
echo '<li><a <img src="'.$thumb[0].'" alt="'.$gallery_image[SN.'gallery_post_title'].'" /><p class="flex-caption">'.$gallery_image[SN.'gallery_post_title'].'</p></li>';
}
}
Your first problem is that. You don't include a function on a string that way.
echo '<li><a href="**<?php the_permalink(); ?>**">
Try this:
echo '<li><a href="'.the_permalink().'">
Then
.$gallery_image[SN.'gallery_post_title'].
You got a syntax error there.
SN.'gallery_post_title' // notice SN
It's fine if you define SN though.
Also, why do you have a close curly brace }?
Did you just copy and paste your code here sloppily or is that intentional? It's confusing if it is.
i have a posting feature on my site that can embed links and youtube videos. the problem is, that the two clash together, and the youtube iframe ends up being a 404 page on my site. my codes for the youtube videos and links are below, but im not sure how to stop them from combining and ruining it.
by combining, i mean this http://www.youtube.com/watch?v=VhqiT2nWCVU turns to
<iframe src="http://www.youtube.com/watch?v=VhqiT2nWCVU">
which then turns into
<iframe src="">
sorry if i am unclear in any way. my codes are below.
function youtube($string)
{
return preg_replace(
'#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
'<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4?rel=0" frameborder="0" allowfullscreen></iframe>',
$string
);
}
$posted = youtube($posted);
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
// Solution 1:
function callback($match)
{
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
return '<a href="' . $completeUrl . '">'
. $match[2] . $match[3] . $match[4] . '</a>';
}
$posted = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",
'callback', $posted);
A popular solution to this problem is to use placeholders. On your first pass you turn all YouTube links into a placeholder, for example:
{{youtube:VhqiT2nWCVU}}
After that you run your normal link converter. And at the end your run yet another regex to turn all your palceholders into youtube embeds.
I'm using the iframe Wordpress plugin. It requires a syntax (shortcode) like:
[iframe src="http://www.youtube.com/embed/A3PDXmYoF5U" width="100%" height="480"]
I want to take the info inside a meta_key and pass in a Wordpress template through the do_shortcode function. The code I'm using is the following:
if ( get_post_meta($post->ID, 'youtube-video', true) ){
$youtube_video = get_post_meta($post->ID, 'youtube-video', true);
echo do_shortcode('[iframe src="' . $youtube_video . '" width="100%" height="480"]');
}
But it doesn't work. I think the problem is due to get_post_meta function that returns a value. How to solve this problem?
EDIT: Please note that if I modify the code above this way:
if ( get_post_meta($post->ID, 'youtube-video', true) ){
$youtube_video = get_post_meta($post->ID, 'youtube-video', true);
echo $youtube_video;
echo do_shortcode('[iframe src="' . $youtube_video . '" width="100%" height="480"]');
}
It print the url (http://www.youtube.com/embed/A3PDXmYoF5U) but the last echo do not print nothing.