Categories
code

Detecting Mobile Devices Accessing Your WordPress Site

Mobile web surfing is continuing to evolve on a larger scale. This is why detecting those users who are visiting your WordPress blog through a mobile device and redirecting them to a mobile version of your site is important. In order to achieve this, you first have to get the code from detectmobilebrowsers.mobi and upload it to your theme directory.

[ad name=”Adsense – text only”]

Then, all you simply have to do is open your header.php file and place the following at the top of the file. Remember to edit line 4 to where you’d like to redirect mobile users.

The Source:

include('mobile_device_detect.php');
$mobile = mobile_device_detect();
if ($mobile){
    header('Location: http://your-website.com/?theme=Your_Mobile_Theme');
}
Categories
code

Human readable and by size sorted disk usage (du) in BASH

This is a quick tip to fix a problem that has always bugged me.
When showing disk usage in a human readable form (KB, MB, GB) for each subdirectory using du -sh *, how can you properly sort it by size.

If you just want the solution here it is as “one-liner”:

function duf {
    du -sk "$@" | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done
}

Just put this function into your ~/.bashrc to make it permanent.

[ad name=”Adsense – text only”]

The expanded code:

function duf {

    // get usage in KBytes and sort
    du -sk "$@" | sort -n | while read size fname;
   
    // loops for each size
    do for unit in k M G T P E Z Y;
   
        // if size<1024 we found the correct suffix
        if [ $size -lt 1024 ];
       
        // display the size
        then echo -e "${size}${unit}\t${fname}";
       
    // line completed
    break;
    fi;
   
    // for each sizes suffix divide by 1024
    size=$((size/1024));
   
    done;
    done
}

When done, you can use your new function like this: duf /*.

Categories
code

Get cross-browser document height in JS

This function will return any document’s height.

[ad name=”Adsense – text only”]

It’s been tested in IE, FF, Safari, Chrome and Opera. If the actual document’s body height is less than the viewport height then it will return the viewport height instead.

The Source:

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}