Find youtube iframe and replace it with custom code - php

I have a wp site that in a lot of posts i am using youtube videos embeded with the clasic iframe.
Is possible to find the iframe div inside post and replace it with a custom youtube code?
I want to use the code bellow to control youtube video. When it starts, when it is finished.
The code that i have in my posts is:
<iframe width="824" height="464" src="//www.youtube.com/embed/eEqIBzxHjiA?rel=0" frameborder="0" allowfullscreen></iframe>
And i want to replace it with:
<script>
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'eEqIBzxHjiA',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert ('FInished');
}
}
</script>
Thank you guys...

use shortcodes
1. The WordPress Rich Editor might strip out JavaScript src tags so best is to write a little plugin that defines a shortcode e.g. [myyoutube id=eEqIBzxHjiA] and use that one from now on. Probably you want to define a javascript .js file that holds the main code and include that in each page where the shortcode is present. So all of the above except the player ID will be in the included .js file that you define in the shortcut code. only the playerId is then a call to the javascript function.
legacy
2. For all the existing entries: probably the fastest to use a sql command on the database posts table and replace all entries matching the regex above with the new shortcode tag. (fastest way)

Related

How to find video source url with simple_html_dom in Php

I started learning php. How can I find video source url with php. Can someone tell me how to get video url with simple_html_dom? For example how to find a video source url from a website?
Thank you
First of all, you need to understand how you can fetch an HTML document on a site. Everything you see in the browser consists of HTML and CSS. When you fetch a HTML document from a page with PHP, you get whatever is on the screen at that moment. So if a content is loaded into the page later using an Ajax call like below, you can't get it directly from the page.
function loadVideoURL() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.status == 200) {
document.getElementById("videosrc").innerHTML = this.responseText;
}
};
xhttp.open("GET", "get_video_url.php", true);
xhttp.send();
}
In the current situation, suppose that the video urls included in the HTML document in the first place.
First Download latest version of simplehtmldom from here. This is the PHP Class to manipulate HTML in easy way and this library requires minimum PHP 5. Create a file which name is simplehtmldomparser.php in main directory of your project and include it using code below to your main php code.
<?php
include 'simplehtmldomparser.php';
?>
This code block will load the class to your main PHP file. Now, you want to fetch video url from the page.
<?php
$html = file_get_html('http://www.videos.com/');
/* This line will fetch HTML document from the site that you wrote. */
?>
Now, you have the html document in $html variable. You need find video tags in HTML document. For example, If fetched page uses a <video> tag for videos, you can get the video urls as follows.
<?php
foreach($html->find('video') as $element){
echo $element->src . '<br>';
}
?>
Here is another example that might work for your project. For example you want to jump another video in the website, fetch all <a> tags and get their hrefs and use the $html = file_get_html($newlyfetchedanchor); again. To fetch all <a> tags from the current $html use code below.
<?php
foreach($html->find('a') as $element){
echo $element->href;
}
?>
There are more useful functions in the class and you can find here.

how to convert every youtube link i have on my wordpress posts into embed?

i am using the latest version of wordpress , i have around 10000 post right now on my website.
i have a nice plugin that makes my website looks a bit more fancey but i need to ad a simple code to each featured image and each embeded youtube video i have in my posts (if i could pick user who add it too that would be perfect ! ) to get it to work
i know that youtube embed code is
<iframe width="560" height="315" src="//www.youtube.com/embed/NrH9CqnzzRU?list=PLHPcpp4e3JVpXbtgyD-k-nCmwpbbMIHOh" frameborder="0" allowfullscreen></iframe>
what i need to do is to add
<div class="featuredvideo"><iframe width="560" height="315" src="//xxxxxxxxxxxxxxxxxxxxxxxxx" frameborder="0" allowfullscreen></iframe></div>
so i want to add those things before and and after "XXXX" s
i am not a programer so please give me the code
i know that AJAX can search entire folder with code like
<button>AJAX</button>
<br>
<div id = "container"></div>
<script type="text/javascript">
$("button").click(function(){
$.get("sample.html",function(data){
$("#container").html(data);
})
});
which will just get html from a file but that is now what i want to do here.
please help me , give me a plugin or a code to work with
thank you
To wrap the Youtube iframes with the div as required, just use this...
$(function() {
$("iframe[src*=www.youtube.com]").wrap("<div class=\"featuredvideo\" />");
});
That will run when the document has finished loading. It will find all iframes with the string www.youtube.com in the src attribute and wrap them in the required div.
Obviously, if you add any more iframes to the page later and it doesn't reload then you'll need to run that code again. If you did just that then it would wrap the iframes in the div again, which would be wrong. This will handle running the code multiple times...
function wrapYoutubeEmbeds() {
$("iframe[src*=www.youtube.com]").each(function() {
if (!$(this).parent().hasClass("featuredvideo")) {
$(this).wrap("<div class=\"featuredvideo\" />");
}
});
}
$(function() {
wrapYoutubeEmbeds();
});
It simply checks to see if each iframe is inside something with the class featuredvideo and ignores it if it is.

Change a css div or body background image based on current URL

The reason I need to do this is that the website I'm working on uses the exact same template to display dynamic content for multiple pages. All pages replicate the exact same div id's because they display the content the same way (except for the header content!). The header content shortens however the div id's still remain within the source code.
The blog index page needs to display 1 background image while every other page on the website displays another background image.
Thanks in advance for any help.
This snippet of code will do what you want:
if (window.location.href.indexOf('somepart_of_the_url') != -1) {
//Change background to some div
$('#somediv').css('backgroundImage','url(images/mybackgroundimage.jpg)');
//Change background to page body
$("document.body").css('backgroundImage','url(images/mybackgroundimage.jpg)');
}
I often give the body class a name based on the template or request path. I know you said that they all use the same template, but that template takes params and one of the params should be
body_class
And whatever controller/dynamic thing you have populating your site and rendering the template, should pass in 'home' when you're at /. In my previous experience, I would pass in other things as well so that /blog/category/post might have a body class like
<body class="post two-column-a">
Then your selectors are something like:
body { ... }
body.home { ... }
This works:
<script language="javascript">
$(document).ready(function() {
if (window.location.href.indexOf('INDEX_URL') != -1) {
//Change background
$('#DIV_ID').css({'background-image': 'url(http://URL.com/images/BG.jpg)', 'background-repeat': 'repeat-x', 'background-position': 'center top', 'width': '100%!important', 'min-height': '400px'});
}
});
</script>
The flaw that this code has though is that if you insert a directory into "INDEX_URL" such as /folder/, any page after /folder/ will have that background.

Print only Image without whole HTML page

I want to print only the image in a HTML page.
I used
function printImg(){
var URL = "image.png";
var W = window.open(URL);
W.window.print();
}
But executing the code, whole page prints rather the image. Are there any method to print only the image? Pls guide me.
Thanks.
create one print.css that hide all other unwanted divs except the image
If you heard about CSS Media types then you can achieve your desired task with this technique.
Example:
Define Media Type Print and then add a css class.noprint with a css rule display:none; in your css file like given below:
#media print {
.noprint{ display: none }
}
And then apply this class on the elements you don't want to print.
SEE AN EXAMPLE
Hope this will help you a lot.
Use
W.print();
instead of
W.window.print();
W.window makes reference to the parent's window of the pop-up.
Besides this you may consider using an empty page with a window.print triggered on load, instead of your current method which is not waiting for the image to be ready.
== EDIT ==
This can't be done. You can't modify browser settings (i.e. printer settings) from JavaScript
Finally I thought of convert my result to PDF format and print using FPDF Library
You can create an image element then append it to the DOM.
function printImg() {
var img = document.createElement( "img" );
img.src = "image.png";
document.body.appendChild( img );
}

Make active div for youtube URL a different shade

I have a page where in the top middle there is a large youtube player. Below it there are a bunch of youtube thumbnails which are clickable. Clicking them changes the player video to the thumbnail that was clicked. The youtube ID is passed thru the URL.
I wanted to change the shade of the thumbnail background so that the active ("clicked") thumbnail was shaded.
The following code generates the linked thumbnails:
while ($row = mysql_fetch_array($result)) {
$tubeID = $row['videoinfo'];
echo
'<div class="vid"><img src="http://img.youtube.com/vi/' . $tubeID . '/0.jpg" width="225" height="175"/></div>';
}
And the following code uses the clicked thumbnail to display the video:
<iframe id="player" width="640" height="385" src="http://www.youtube.com/embed/<?php echo $_GET['id'] ?>" frameborder="0"></iframe>
I know I need to compare the thumbnail URL to the current URL and if they match and set an ID to that thumbnail which can be assigned properties in CSS... though I'm not sure how to do so. Can someone help?
edit : this sorta explain what BZ suggested.
I kicked out some php to make the answer more concise. Don't forget to add it back.
<div class="vid">
<a href="videos.php?id=$tubeID" id="m+$tubeID" onclick="changeVid(this);return false;">
<img src="http://img.youtube.com/vi/01/0.jpg" width="225" height="175"/>
</a>
</div>
<script type="text/javascript>
function changeVid(obj) {
document.getElementById('player').src = "videos.php?id="+ substr(obj.id,1)
document.getElementById(obj.id).style.visibility='visible'; //or other style
document.getElementById(obj.id).setAttribute("class", newClass); //For Most Browsers
document.getElementById(obj.id).setAttribute("className", newClass); //For IE; harmless to other browsers.
}
</script>
of course, I'd be a lot easier with a JS framework... (ie jquery)
Basically, when you click a link, the function is called, when it's done, return false; disable the page loading (ignoring the href).
The function will change the iframe source and then will add some custom styling to the link you clicked to load the new video.
I've thrown a "m" in the link ID, because id can't start with numbers...
edit : my function changeVid lack some reset function to remove the old thumbnail active state (easy way to solve it : remove the active state from all thumbnail then put the active state on the clicked thumbnail.)
In each link you could put an onclick attribute to do some javascript that would do the highlighting.

Categories