Archive

Author Archive
jobs

WordPress: Embed Youtube Feed With a Simple Shortcode

August 13th, 2010home Andrew 19 comments

WordPress shortcodes are great. They let you perform complex operations or display dynamic content with just a tiny bit of text in any page or post.

I recently needed a way to embed the most recent videos from a Youtube channel (very dynamic information) in a WordPress page, so I decided to write a shortcode for it.

The code is actually pretty simple. Just add the following to your theme’s functions.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function youtube_feed_shortcode($atts)
{
    // Defaults:
    extract(shortcode_atts(array(
            'user' => 'flamadiddle86', // youtube user
            'limit' => 5, // maximum number of videos
            'height' => 385, // video height
            'width' => 480 // video width
        ), $atts));
    $data = @json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/users/'.$user.'/uploads?alt=json'), TRUE);
    $counter = 0;
    $content = '<div class="youtubefeed">';
    foreach($data['feed']['entry'] as $vid)
    {
        $url = $vid['media$group']['media$content'][0]['url'];
        $title = $vid['title']['$t'];
        $ycontent = $vid['content']['$t'];
        $content.= '<object width="'.$width.'" height="'.$height.'">'.
            '<param name="movie" value="'.$url.'"></param>'.
            '<param name="allowFullScreen" value="true"></param>'.
            '<param name="allowscriptaccess" value="always"></param>'.
            '<embed src="'.$url.'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed></object>'.
            '<div class="youtubetitle">'.$title.'</div>'.
            '<div class="youtubecontent">'.$ycontent.'</div>'."\n";
        $counter++;
        if($counter == $limit)
        {
            break;
        }
    }
    $content .= '</div>';
    return $content;
}

add_shortcode('youtubefeed', 'youtube_feed_shortcode');

And it couldn’t be easier to use. The videos you see below were embedded simply by putting this code in the post:

1
[youtubefeed]

Read more…

information

Recover WordPress from broken theme

April 26th, 2010 Andrew No comments

If you ever find yourself with a broken theme and can’t even access the admin side of your wordpress blog, here’s a quick way to get back on your feet.

Simply run the following query from the command line or via a tool like phpMyAdmin

UPDATE wp_options SET option_value = 'default'
WHERE option_name IN ('template','stylesheet','current_theme');

(Make sure to select your wordpress database first, of course)

Sure. Your site looks like it was just setup 2 seconds ago, but at least now you can access all the GUI-licious tools of the WordPress Admin panel to fix whatever broke your theme.

handbook

PHP: Validate an IP Address

March 13th, 2010copyright Andrew No comments

So you need to check if some string is a valid IP address. You could simply test it against a regular expression:

1
2
3
4
5
6
7
function is_valid_ipv4($ip)
{
    return preg_match('/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.
        '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.
        '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.
        '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/', $ip) !== 0;
}

Regular Expression obtained here

This will actually work for most situations, but it’s lacking in a few ways. Suppose you want to exclude private or reserved IP addresses. Maybe you want to validate IPv6 addresses too; not just IPv4.

Enter PHP’s Data Filtering Extension. It just works, and you don’t have to worry about maintaining (or properly applying) complex regular expressions.

Read more…

address
marketing
careers