Get All Google Web Fonts

Google Web Fonts is a wonderfully easy way to add beautiful fonts to your website using CSS only, if you’re still using cufon, shame on you!

Using Google Web Fonts is very easy, but if you build themes or similar things you might want to show your users a list of available fonts. It turns out this is pretty easy to do, just get the contents of the following url.

To get an API key you might want to visit the Developer Console. From here on out it’s a trivial matter to present the data, especially since the data is returned in json.

$fonts = json_decode( 
        file_get_contents( '' . WEBFONTS_API_KEY ) 

Avoiding Counters In Loops

When writing code I try to keep things clean but one thing has always bothered me, using $i (or something similar) as a counter, incrementing it after each step. Here’s an alternative approach which you can use with arrays to get around those pesky counters.

$navigation = array( 'Home', 'Contact', 'About' );

foreach( $item = current( $navigation ) ) {
    $current = ( key( $navigation ) == 0 ) ? 'current' : '';
    echo '<a class="' . $current . '" href="#">' . $item . '</a>';
    next( $item );

While not much shorter, it is much better because the current value is now inherently contained inside the actual array keys. If something doesn’t work here you can be pretty sure the problem is in the loop and if it seems to work, you can be pretty sure it actually does.

If you haven’t seen this before take a look at the current() function on the PHP codex, it has all the similar functions listed below for moving pointers and such, very handy!

WordPress Pagination Links

For ages and I ages I used a plugin for WordPress pagination when there has been a solution since 2.1 to do this easily! Don’t get me wrong, wp_pagenavi is a great plugin but I prefer not to use plugins needlessly.

The paginate_links() function allows you to create easy-to-customize pagination which you can use outside the loop as well.

$list = new WP_Query($query_args);
$pagination = array(
	'base'       => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ),
	'format'     => '?paged=%#%',
	'current'    => max( 1, get_query_var( 'paged' ) ),
	'total'      => $list->max_num_pages,
	'next_text'  => 'next',
	'prev_text'  => 'previous'			
echo '<div class="pagination primary-links">';
echo paginate_links( $pagination );
echo '</div>';

Since the function does not use the loop directly you can use it for listing anything as long as you know the current page and the total number of pages.

$list = range(1, 200);
$items_per_page = 12;
$pagination = array(
	'base'       => get_bloginfo( 'url' ) . '/mypage/%_%',
	'format'     => '?paged=%#%',
	'current'    => $_GET['current_page'],
	'total'      => ceil( max($list) / $items_per_page ),
	'next_text'  => 'go forth',
	'prev_text'  => 'go back'			
echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';