Archive for the ‘Technical solutions’ category

Common use snippets for Widget Logic

17/07/2012

Widget Logic is a WordPress plugin that that gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.

I’ve been using Widget Logic in my WordPress projects for a while now – it’s clean, versatile and I highly recommend it.

Below are some snippets that you will likely find useful when you are building a new site.

Show widget only on specific pages or posts

This example displays the widget only on the page that has the slug example-page

is_page('example-slug')

This is my favourite way of specifying widgets for certain pages, as it works in a similar fashion to how you configure blocks for specific paths in Drupal.

This example displays the widget on pages with ID 1 and 5.

is_page(array(1,5))

is_page() is very flexible and can take id’s, page names, slugs and even arrays with multiple values – see the documentation.

But what about blog posts and attachments?

Use is_single()  instead – see the documentation.

Here is an example that display the widget when the user is visiting a post with the title “About this blog”

is_single('About this blog')

Show widget everywhere except a specific page or post.

This is the inverse of the previous examples, so the first example would be:

!is_page('example-slug')

Note the exclamation point, which inverts the logic.

The second example would be:

!is_page(array(1,5))

Show widget only on front page

Handy for front page-specific summary widgets, calendars, introductions etc.

is_front_page()

Show widget only for a specific post type

An easy way to display widgets for certain post types.

get_post_type()=='post'

Some common post types

  • page
  • post
  • attachment

Show widget based on a WPML language

If you are running a site using the WordPress Multilingual Plugin (WPML) you can use this snippet to only show a widget when the user is viewing the page in a certain language (English in this case)

(ICL_LANGUAGE_CODE == 'en')

There are more useful constants and functions available in the WPML manual.

Combining multiple rules

It’s easy to combine multiple rules with regular PHP logic. This example will display the widget only when the user is viewing a site in Swedish (WPML) and is on the pages with ID 1 or 5:

(ICL_LANGUAGE_CODE == 'sv') && is_page(array(1,5))

Don’t forget you can also use the || (or) operator!

More useful tags

I recommend reading the WordPress documentation on Conditional Tags to customize Widget Logic for your exact needs.

Feedback!

Do you have a useful snippets for Widget Logic? Feel free to post it in the comments!

Using CodeIgniter 2 with SQLite 3 databases

22/06/2012

Note: If you haven’t updated to CodeIgniter 2.1.1 – do so. There is a bug in 2.1 preventing it from working with SQLite databases properly, see the changelog.

The process of connecting to a SQLite 3 database with CodeIgniter isn’t straightforward. You have to use the (built-in) PDO driver and put the path to the database, with protocol prefix, in the hostname parameter, here’s an example:

/application/config/database.php

...
$db['default']['hostname'] = 'sqlite:'.APPPATH.'db/your_database.sqlite';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'pdo';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
...

This assumes you have your database under:

/application/db/your_database.sqlite

If the file does not exist, an empty database will be created at that location.

After you have connected, you can use the Active Record class normally to query the db, for example:

$query = $this->db->get('users');
foreach ($query->result() as $row)
{
    echo $row->id . '<br/>';
}

Remember that you need to build your table structures first with a tool such as phpLiteAdmin.


Why does the syntax differ when conecting to a SQLite 3 database?

The reason you have to put the database path in the hostname field is the way CI instantiates the PDO object:

system/database/drivers/pdo/pdo_driver.php

...
function db_connect()
{
    $this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT;
    return new PDO($this->hostname, $this->username, $this->password, $this->options);
}
...

Minimal server online status checker (HTML/JavaScript)

07/02/2012

Starbuck, noooooo!

Above is an example of a nifty server status script I have been using in a recent project.

Here is the base code for monitoring a web server:

<img src="http://site-to-check.com/online.png" alt="status" onerror="this.src='files/offline.png'" />

The setup is:

  • An online icon (online.png) on the remote server you want to see the status of.
  • An offline icon (offline.png) on the server that is going to show the status page.

If the image on the remote server fails to load, the onerror event is triggered and the javascript rewrites the image tag to show the offline image. You can use relative or absolute paths for the offline image. This snippet is fully compatible with all major browsers, including Internet Explorer down to version 5.5!

Demo
Click here for a demo.

Resources
Below are the images I use, found on Clker, an excellent online clipart resource.

Online
Offline

Participate
If you find this useful or have any suggestions for improving it, feel free to write in the comments!

Updating LoopiaDNS from Tomato and DD-WRT firmware

30/01/2012

Loopia is one of the largest Swedish hosting providers and offers a free dynamic DNS service for any domain you own.

They have a great API which can be updated with a number of dynamic DNS clients. They also provide the possibility to update via a HTTP GET request, which we can use in Tomato and DD-WRT.

Tomato
Go to Basic -> DDNS, select Custom URL as service and enter the URL below, swapping username, password and domain.com for your own values:

https://username:password@dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname=domain.com&myip=@IP

If you want to update multiple hostnames to the routers IP address, you can use comma delimited values in hostname, for example:

hostname=domain.com,sub.domain.com,anotherdomain.com

Example:

If you have a wildcard record (*.domain.com) it will get wiped, unless you append

&wildcard=NOCHG

to your request. Read this page for more info. (Swedish)

DD-WRT

Updating LoopiaDNS from DD-WRT can be done via a startup script, check this forum post for instructions.

You can visit https://www.loopia.com/loopiadns/ for information about LoopiaDNS in english.

Tweaking Bulletstorm for PC

29/12/2011

A couple of days ago I picked up Bulletstorm during the Steam Holiday Sale.

Bulletstorm is an excellent shooter, but it has been given a pretty bad PC port.

Luckily, since it’s based on the Unreal engine (UDK) you can tweak away any shortcomings using the configuration files.

First, you will need a tool to edit the obfuscated config files. Here are some mirrors for it:
Mirror 1 Removed due to DMCA request
Mirror 2

On Windows 7, the configuration files are located in:

C:\Users\<your account name>\Documents\My Games\BulletStorm

Important: If you use the tool above to open the configuration files directly from this folder, you need to run the tool with administrative privileges.

Now, start out with this excellent article by Ars Technica. It will help you fix things such as Mouse Smoothing, locked framerates and poor field-of-view in the game.

After this, there is one more important bug to fix – the scope view.

By default, the scope view (a.k.a. the aim down sight) has a fixed sensitivity that is unaffected by the in-game sensitivity setting. This means that if your in-game sensitivity is set very low, for example due to a high DPI mouse, the scope view will be too sensitive.

To fix this, we will need to adjust the internal Unreal engine mouse calibration.

In StormInput.ini, find the line MouseSensitivity and change it to a lower value, like this: (Default is 60)

MouseSensitivity=10

This setting affects both the scope view and the regular sensitivity, so you will also have to increase the sensitivity in-game after this tweak.

Here are the settings I run in-game. Of course, the graphical settings are subject to your card.

Speaking of Bulletstorm…
Some UI design choices in this game are really weird, here’s one that stumped me the first time, let’s see if you spot it: