Get YouTube Video Data with PHP

This is amazing simple. I always thought oh no another API and if its anything like Facebooks API then you have to learn something new every month! But this is amazingly easy!

You just need to pull the information you want from You Tube via ‘simplexml‘ in PHP

YouTube has loads of different RSS feeds, in this case I am just using the one that gets all data about a single video.

//your video id

// get xml
$video_feed = file_get_contents("$v");
$sxml = new SimpleXmlElement($video_feed);

//set up nodes
$namespaces = $sxml->getNameSpaces(true);
$media = $sxml->children($namespaces[‘media’]);
$yt = $media->children($namespaces[‘yt’]);
$yt_attrs = $yt->duration->attributes();

$video_title = $sxml->title;
$video_description = nl2br($sxml->content);
$video_keywords = $media->group->keywords;
$video_length = $yt_attrs[‘seconds’];

Return string between two characters in the given string PHP

Sometimes I find it useful to return only a certain part of a string identified by two characters in that existing string.

Here is the useful function to achieve this:

function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);

How to Make your Page Title Different From your Menu Title in Word Press

Word press by default takes the title of your page and uses it for the menu title too. This can be annoying if you want to have say “Welcome to my Website” as your title on your page but only reference it as “Home” on the menu bar.

So I hear you say ‘why not create a custom menu as you can label the menu items?’ Yes you could but from my experience if you are dealing with clients who don’t really know what they are doing you need to make it as simple as possible. The custom menu doesn’t automatically update the menu items when new pages are added by default. With my method the menu is still updated automatically.

This is actually really easy to solve with a little bit of PHP and using the custom fields section when making your page.

So first create your page and call it ‘Home‘  this will be used as the menu item name. Now make a custom field on your page called ‘pagetitle‘ and add the value you want for your main page title.

Now open your ‘page.php‘ file and add the following code;

First at the top add this line, which gets your custom field and assign it to the var ‘$meta_values

[sourcecode language=’php’]

ID, “pagetitle”, true); ?>


Now find any reference to ‘the_title();‘ usualy found in <h2> and <h1> tags and replace with the following code;

[sourcecode language=’php’]


What the above does is checks if you have actually made a custom field for that page, if true it will use that field as the main page title. If not true it will just use the same value you originally named your page. This way you don’t have to add a custom field to all your pages, only the ones you want to have different page titles too.