Why does the blog look terrible? Find out
mikestreety~coding & life

Archives

Strip out YouTube code from the URL

Big thanks to Kris Noble for this snippet of code. If you are setting up a CMS for a client which includes a video of sorts – you may wish to keep the ratio and dimensions, but allow them to change the code through the CMS. This function below strips out the required code from …

Big thanks to Kris Noble for this snippet of code.

If you are setting up a CMS for a client which includes a video of sorts – you may wish to keep the ratio and dimensions, but allow them to change the code through the CMS. This function below strips out the required code from the URL so that the client doesn’t have to. If the client does paste in just the code – it picks that up as well and uses it.

[php]
function get_youtube_id($ytURL)
{
// Adapted from http://snipplr.com/view.php?codeview&id=19232

$ytvIDlen = 11; // This is the length of YouTube’s video IDs

$ytURL = str_replace(‘http://youtu.be/’, ”, $ytURL);
// Accounts for short youtube URL

if(strlen($ytURL) == $ytvIDlen)
{
// probably already a valid id
return $ytURL;
}

// The ID string starts after "v=", which is usually right after
// "youtube.com/watch?" in the URL
$idStarts = strpos($ytURL, "?v=");

// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
// bases covered), it will be after an "&":
if($idStarts === FALSE)
{
$idStarts = strpos($ytURL, "&v=");
}

// If still FALSE, URL doesn’t have a vid ID
if($idStarts === FALSE)
{
// some kind of ‘Please enter a valid YouTube video ID or URL’ validation message here maybe..
return FALSE;
}

// Offset the start location to match the beginning of the ID string
$idStarts +=3;

// Get the ID string and return it
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);

return $ytvID;

}
[/php]

For example, in your CMS you might have a field titled ‘Youtube Video’ where the user pastes in the URL to the video.

On your front end you will then have code similar to this:

[php]
<?php $videoID = get_youtube_id($youtubeVideo);
if($videoID) { ?>
<iframe width="357" height="222" src="http://www.youtube.com/embed/<?=$videoID?>" frameborder="0" allowfullscreen></iframe>
<?php } ?>
[/php]

In other words – if there is a youtube video content, show the above code and place the youtube video ID in the correct place.

Below is a minified version of the function

[php]
function get_youtube_id($ytURL) {
$ytvIDlen = 11;
$ytURL = str_replace(‘http://youtu.be/’, ”, $ytURL);
if(strlen($ytURL) == $ytvIDlen) {return $ytURL;}
$idStarts = strpos($ytURL, "?v=");
if($idStarts === FALSE) { $idStarts = strpos($ytURL, "&v=");}
if($idStarts === FALSE) { return FALSE;}
$idStarts +=3;
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
// Thanks to Kris Noble and http://snipplr.com/view.php?codeview&id=19232 for this
}
[/php]

Read More

iFrame Reveal Code/Like to Win/Like to Download Tab for Facebook

This code is now no longer supported – a new blog post will be coming soon [php]<?php function parsePageSignedRequest() { if (isset($_REQUEST['signed_request'])) { $encoded_sig = null; $payload = null; list($encoded_sig, $payload) = explode(‘.’, $_REQUEST['signed_request'], 2); $sig = base64_decode(strtr($encoded_sig, ‘-_’, ‘+/’)); $data = json_decode(base64_decode(strtr($payload, ‘-_’, ‘+/’), true)); return $data; } return false; } if($signed_request = parsePageSignedRequest()) …

This code is now no longer supported – a new blog post will be coming soon

[php]<?php
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode(‘.’, $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, ‘-_’, ‘+/’));
$data = json_decode(base64_decode(strtr($payload, ‘-_’, ‘+/’), true));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) { ?>

YOU LIKE THE PAGE

<?php } else { ?>

LIKE THE PAGE

<?php } ?>
[/php]

Read More

Widgetizing a WordPress Sidebar and Creating an Additional One

When developing a WordPress theme, you may wish to take advantage of the built in system to manage, edit and order the sidebar widgets. Many themes have this built in, but if you’re developing from scratch, you might not have the code to implement it. First, locate your functions.php. This can be found in: wp-contentthemesYOUR …

When developing a WordPress theme, you may wish to take advantage of the built in system to manage, edit and order the sidebar widgets. Many themes have this built in, but if you’re developing from scratch, you might not have the code to implement it.

First, locate your functions.php. This can be found in:

wp-contentthemesYOUR THEME NAMEfunctions.php

If this file doesn’t exist, then simply make one starting and finishing with the standard php tags

?>

In between the php tags, paste the following code:

[PHP]
/* ADDING SIDEBAR WIDGETS FUNCTIONS */
if ( function_exists(‘register_sidebar’) )
register_sidebar(array(
‘before_widget’ => ‘

‘, //what each widget instance is wrapped in
‘after_widget’ => ‘

‘, //closing tag of the above
‘before_title’ => ‘

‘, //what the widget title is wrapped in
‘after_title’ => ‘

‘, //closing tag of the above
));
[/PHP]

This code (commented appropriately) is what wraps the widget and it’s heading. The %2$s adds a class specific to the widget.

Next, head to sidebar.php or wherever you want your widgets to appear. Paste in the following code:

[HTML]

[/HTML]

The alternative content will only appear if there are no widgets in the sidebar.

If you want to create a second widget section (be it in your footer, or in a second sidebar) then you will need the following code in your functions.php

[PHP]
if ( function_exists(‘register_sidebar’) )
register_sidebar(array(
‘name’=>’after_posts’,
‘before_widget’ => ‘

‘,
‘after_widget’ => ‘

‘,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
));
[/PHP]

The only new code here is ‘name’. Which tells WordPress what the name of your new sidebar is. Now head over to the file where you want this second widget section to be and paste in the following code:

[HTML]
|| !dynamic_sidebar('after_posts') ) : ?>

[/HTML]

Change ‘after_posts’ in both bits of code accordingly and you are good to go!

Read More

Limit Items (e.g. News posts)

If you would like to limit the amount of items coming out of an array (for example the first 5 news items), then before your loop (e.g. while), specify your start and finish points [PHP] $A = array_slice($A, START, END); while $A { [/PHP] For example, if you did want to only pull out the …

If you would like to limit the amount of items coming out of an array (for example the first 5 news items), then before your loop (e.g. while), specify your start and finish points

[PHP]
$A = array_slice($A, START, END);
while $A {
[/PHP]

For example, if you did want to only pull out the first 5 news items in an array, you would:

[PHP]
$news_items = array_slice($news_items, 0, 5);
while $news_items {
[/PHP]

Read More

Get an ‘Even’ Class

This simple PHP statement applies class=”even” to every other element when in a loop. I often use this on tables to get an even class to help devide up the rows, but it can also be used to add a class to the 4th item in a list (if you want to remove certain margin/padding …

This simple PHP statement applies class=”even” to every other element when in a loop.

I often use this on tables to get an even class to help devide up the rows, but it can also be used to add a class to the 4th item in a list (if you want to remove certain margin/padding for example).

To start, ‘clear’ i and make it 0. (if $i is already used elsewhere in your code, you can replace it with anything)

[PHP]$i=0;[/PHP]

Then once in the loop, use/adapt the following code to achieve the desired result (have broken it up into seperate lines to add comments. The single line code is included after)

[PHP]
echo $i++ % 2 ? //if $i divided by 2 has no remainder
' class="even"' : //then echo this result
''; //if not echo this result
?>
[/PHP]

the number 2 can be replaced with what ever number item you want the class. And if you already have existing classes on your item, then remove the class=”" and just have the class name.

The code as a single line:

[PHP]

[/PHP]

Read More

Giving something a ‘Last’ Class

If you want to apply a different class to the last item in a list generated by PHP, start off with Setting the $lastOne variable to the end of your list [php]$lastOne = end($A);[/php] This stores the details of the last one in your list into the lastOne variable – we’ll compare later. You may …

If you want to apply a different class to the last item in a list generated by PHP, start off with Setting the $lastOne variable to the end of your list

[php]$lastOne = end($A);[/php]

This stores the details of the last one in your list into the lastOne variable – we’ll compare later.

You may need to modify your existing while/foreach loop, adding in the $i variable to your item. If i is already used, then pick any other letter.

[php]foreach($A as $i => $B){[/php]

Then on the list item or table row that you want to apply the last class to, compare your $lastOne with the current one your are looping through.

[php][/php]

To give you an real life example, the below was used on a project while listing out related categories to the one the user was currently browsing. We experienced a recurrence loop and so to overcome this, we simply compare the unique ID (primary key) field for ease and to bypass any problems

[php]$lastOne = end($relatedCategories);
foreach($relatedCategories as $i => $relCat){

uid==$relCat->uid?’ last’:”?>[/php]

Read More

Develop, Style or Add Content to a Live Site

If you want to develop/edit something on a live website, but you want to send a preview to a client, it can sometimes be tricky weighing up where and how to do it. If your website is PHP, there is a simple piece of code you can implement to send someone a test URL. Copy …

If you want to develop/edit something on a live website, but you want to send a preview to a client, it can sometimes be tricky weighing up where and how to do it.

If your website is PHP, there is a simple piece of code you can implement to send someone a test URL. Copy and paste the below code into wherever you need to add/change.

[php]<?php if($_SERVER['QUERY_STRING'] == ‘test’) { ?>
<!– code protected from public viewing –>
<?php } ?>[/php]

You can put anything in between the PHP, Including CSS, HTML and PHP. If you need to replace something rather than add, simply adapt it to be an ELSE statement like so:

[php]<?php if($_SERVER['QUERY_STRING'] == ‘test’) { ?>
<!– code protected from public viewing –>
<?php } else { ?>
<!– Original Code –>
<?php } ?>
[/php]

To access the extra content, navigate to www.yoursite.com/anypage?test. To change ‘?test’ to any other word, edit the first line of the PHP.

This is also helpful for use with contact forms (sending the user back to your contact page with ?thanks in the URL or similar and replacing the contact form with a message), or sending users to different links on the same page to reveal codes or links.

Read More