How to add Google fonts to WordPress themes

One of the recurring errors I see during theme code reviews is incorrect inclusion of Google fonts. There are five things that I look for in particular:

  1. Is the font enqueued instead of included directly in the template files or the CSS?
  2. Is the font enqueued on the correct hook?
  3. Is the font URL protocol independent?
  4. Can translators deactivate the font if their language’s character set isn’t supported?
  5. Can the font easily be dequeued by child themes?

In this post we’ll see how we can do it right when it comes to adding Google fonts to themes. How to add Google fonts to WordPress themesContinue reading

Underused WordPress functions: checked(), selected() and disabled()

If you work with forms in PHP, you often have to write code like this snippet:

<option value="<?php echo esc_attr( $value ) ?>" <?php echo ($value == $other_value) ? 'selected' : '' ?>>

Luckily WordPress provides three functions that help with such tasks:

  • checked() for radio buttons and checkboxes.
  • selected() for options in select elements.
  • disabled() for input elements that can be disabled.

All three functions use the same private helper function (namely __checked_selected_helper). This means the way they work, as well as their parameters, are identical:

  • $value (required): The value that you want to compare.
  • $current (optional): Value to compare to. Defaults to true if not set.
  • $echo (optional): Output or return the generated HTML. Defaults to true if not set.

You either pass in a single value, or two values. If the comparison returns true, the correct HTML will either be outputted or returned, depending on the third parameter.

Let’s have a look at some code examples.

<input type="checkbox" name="x" value="1" <?php checked( $x ); ?> /> Is x true?

In this case if $x contains a value that evaluates to true, checked() outputs checked="checked". If $x evaluates to false, nothing will be outputted and the checkbox won’t be checked.

<select name="fruit">
    <option value="apple" <?php selected( $fruit, 'apple' )?>>Apple</option>
    <option value="orange" <?php selected( $fruit, 'orange' )?>>Orange</option>

Here we compare two values to determine whether an option needs to be selected or not. We use a string here as second parameter, but it could be a variable as well.

$x = 1;
$y = 2;
$string = '<input type="radio" name="math" value="1"' . disabled( $x, $y, false ) . ' />';
echo $string;

In this third example, we have passed two variables to the disabled() function and passed false as third parameter. This will return the HTML generated by the function instead of outputting it.

As $x and $y are not equal, the radio button is active. If $x and $y were equal, the button would be disabled.

There you have it! Three functions that save keystrokes and make your code a lot cleaner.

Don’t Call Yourself A Programmer, And Other Career Advice

While the internet is full of advice on the technical side of making a living by writing code, there is little good advice to be found about how to build your career in the field.

Patrick McKenzie’s Don’t Call Yourself A Programmer, And Other Career Advice is an exception to this.

Whether you are starting out fresh after getting your degree or whether you’ve been around for a few years, the article is full of useful advice for software engineers.

Getting Started With Swift: Tools and Ressources

During the WWDC 2014 keynote, Apple introduced a new programming language called Swift.

In comparison to Objective-C, Swift is much closer to modern programming languages such as Ruby. This makes Swift easier to learn for people that have a web development background.

So if you are interested in iOS app development, but have been hesitant to learn Objective-C, now is the ideal moment to dive in.

Here is a list of resources to help you learn this new language.

Required Software

As of right now, Swift is only available to registered Apple developers. To download it, you need to enroll in Apple’s Developer Program. The yearly fee for the iOS Developer Program is $99.

Once you have a valid account, you can download the beta of the Xcode 6 IDE. This software contains all the necessary tools to write and compile Swift programs.

Documentation and Tutorials

Projects and Code Samples

Erik Spiekermann on Unreadable Typography

While the above video is named “Erik Spiekermann on iOS 7″, it goes far further than that.

Erik Spiekermann uses this opportunity to address the lack of readability in contemporary screen design. I consider his remarks to be spot on, I’ve seen the “unreadable carpet of text” all too often.

The video is in German, but you can use the subtitles to get the English translation.

Keep Featured Content Posts from Being Exluded from the Blog in Twenty Fourteen

Twenty Fourteen has a Featured Content area on the front page. This allows you to feature certain posts in a slider or a grid view.

The content for this area is selected based on an configurable tag. These posts are then excluded from the blog view, to avoid duplicates.

But what if you don’t want the featured posts to be filtered out?

Keep Featured Content Posts from Being Exluded from the Blog in Twenty FourteenContinue reading

Fundamentals of Theme Development

As a theme developer, you are building upon the tools that WordPress Core provides you. These tools are things like the Template Hierarchy, Template Tags and The Loop.

Knowing how to use these tools in an effective manner is great. But knowing how these tools work is even better.

In his presentation Fundamentals of Theme Development, Kirk Wight gives you an insight into “What happens behind the scenes in core to make themes do what they do”.

A must watch for any WordPress developer.