I take the chance to promote a one-pager I just published: WordPress Theme Development Cheatsheet.
It contains handy snippets for reference while making awesome WordPress themes.
Any suggestions/modifications are welcome! =)
I take the chance to promote a one-pager I just published: WordPress Theme Development Cheatsheet.
It contains handy snippets for reference while making awesome WordPress themes.
Any suggestions/modifications are welcome! =)
Want to display a list of recent plays in last.fm on your website? Last.fm API makes it easy. To start you need to have an API key, you can do that here. Copy your API key and then let’s get on with the code.
No authentication is needed. Remember to add your own API key.
$api_key = '';
$username = 'codeclown';
$limit = 3;
/* Construct the url */
$url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.$username.'&api_key='.$api_key.'&limit='.$limit;
/* Make a cURL-request (cURL is prefered over e.g. `file_get_contents`) */
$curl = curl_init();
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($curl);
curl_close($curl);
/* Process the results with `SimpleXMLElement` */
$xml = new SimpleXMLElement($result);
foreach($xml->recenttracks->track as $track) {
/* Data in variables */
$artist = $track->artist;
$name = $track->name;
$album = $track->album;
$lastfm_url = $track->url;
/* This does not exist, if the track is being currently played */
$date = $track->date;
/* An array that contains 4 different sizes */
$albumart = $track->image;
/* Nowplaying is embedded in attributes */
$nowplaying = (bool) $track->attributes()->nowplaying;
/* Use the data here */
echo $artist.' - '.$name.'<hr />';
}
This is very useful snippet, that removes the situation in which a header happens to be a bit too wide, and the last word will wrap to next line. This piece of code add a non-breaking space ( ) between the last two words, so that if something wraps, it’s two words instead of one.
$title = preg_replace('/(.*)\s([^\s]+)/', '$1 $2', $original_title);
If you don’t have an idea what I’m talking about, see this fiddle.
Imagine you have an object with numerous properties, and you want to have easy access to them in your code while ensuring encapsulation for these properties. Like in the snippet below, where you want to ensure that only strings can be assigned to name:
$test = new Test();
$test->name = 'This is my name!';
$test->name = 500; // name can't be an int!
This can be easily accomplished by using the magic methods __get() and __set() of an object. In the code below, the properties of the class that extend Base can be accessed if a getter or setter method has been declared for that property. The getter or setter is simply a method with the same name as the property, prefixed with get or set.
This way you can restrict the values being assigned to these properties, and alter the values being returned by the getters.
The full example:
class Base {
public function __get($name) {
$method = "get$name";
$this->checkPropertyAccess($method, $name);
return $this->$method();
}
public function __set($name, $value) {
$method = "set$name";
$this->checkPropertyAccess($method, $name);
return $this->$method($value);
}
private function checkPropertyAccess($method, $property) {
if (!method_exists($this, $method)) {
throw new Exception("Property '$property' not accessible");
}
}
}
class Test extends Base {
private $_name;
public function getName() {
return $this->_name;
}
public function setName($name) {
if (is_string($name)) {
$this->_name = $name;
}
}
}
$test = new Test();
$test->name = 'This is my name!';
$test->name = 500;
echo $test->name;
Menu’s in WordPress are quite customizable, items can be added and removed. Imagine you have a menu that must always include certain options, but you want to prevent them from being accidentally removed by the user. This can be done by using the wp_nav_menu_items filter. All the code for this goes into your theme’s functions.php.
First, make sure your menu is registered and included in the page:
register_nav_menus(array(
'main-menu' => __('Main Navigation')
));
wp_nav_menu(array(
'theme_location' => 'main-menu',
'menu_id' => 'main-menu'
));
Next we’re going to add a filter:
add_filter('wp_nav_menu_items', 'custom_nav_menu_items', 10, 2);
The filter is executed for each menu on the page. The first argument is the name of the filter, the second the name of our function that will add the menu item. The third is the priority, ten by default, and the final one is the number of arguments our custom function expects.
The custom_nav_menu_items function is implemented like this:
function custom_nav_menu_items($items, $args) {
if ($args->theme_location == 'main-menu') {
$home = '<li><a href="' . home_url() . '">Home</a></li>';
return $home . $items;
}
return $items;
}
We’re expecting two arguments: $items is the HTML for the other menu items, and $args contains information about the menu. $args->theme_location is used to make sure the item is added to a specific menu on the page. Note how $home is prepended to $items, if it were appended it would show as the last option in the menu.
That’s it! Now refresh the page and you’ll see the ‘Home’ option appearing.