#snippets


Separate Pings/Trackbacks from Total Comment Count in WordPress

After tak­ing a look at some search results and at Matt Martz’ arti­cle, Sep­a­rat­ing Pings from Com­ments in Word­Press 2.7, I still had some issues with par­tic­u­lar­ly the com­ment count. There were also a prob­lem with how the func­tions I have added/modified are being car­ried across in my tem­plates (but prob­a­bly are most­ly in the scope of this Blue­print theme). The files that we’re most­ly going to deal with are these ones: comments.php, functions.php and single.php.

Any­ways, I just want to note here how I took care of them and what I end­ed up with.

Con­tin­ue read­ing →

Resize to a Specific Element’s Dimensions

Here’s a snip­pet to resize a back­ground image to the dimen­sions of an ele­ment. In this case, #bkgIm­age.

var resizeBkg = function() { 
    var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 
    var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 
    var e = document.getElementById('bkgImage'); 
    if (w > h) { 
        e.width = w; 
        e.height = w; 
    } else { 
        e.height = h; 
        e.width = h; 
    } 
} 

Find Duplicates in Address Book

Ever had the sit­u­a­tion where you have synced your iPhone (to iTunes OS X) and hap­pen to sync both the con­tacts from your Address Book and Google Con­tacts? Or, just hap­pen to add some­one’s new con­tact info as anoth­er con­tact-card? Well, here’s how I cleaned and merged dupli­cate con­tacts so that my Address Book per­forms like a champ.

  1. Launch Script Edi­tor (Applications/AppleScript/Script Edi­tor)
  2. Paste the fol­low­ing code snip­pet
    tell application "Address Book"
        set biglist to {}
        set theGroup to count every person
        if not (exists (group "Duplicate Entries")) then
            make new group with properties {name:"Duplicate Entries"}
        end if
        set the_names to the name of every person as list
        repeat with i from 1 to number of items in the_names
            set this_Name to item i of the_names
            set theName to name of person this_Name as string
            if this_Name is not in biglist then
                copy this_Name to end of biglist
            else
                add (people whose name is theName) to group "Duplicate Entries"
            end if
        end repeat
        save addressbook
    end tell
  3. Save your new­ly cre­at­ed script (would usu­al­ly default to ~/Documents/AppleScripts/), and just open/run it

Now, the eas­i­est way to fig­ure out if its run­ning or not (at least for me) was to run the Activ­i­ty Mon­i­tor and sort by CPU. I sim­ply just wait­ed to see if Address Book would climb to the top doing 90+, and back down to nil. When this is done, you should be able to see a group called “Dupli­cate Entries”. From there, you may now sort through the details and delete the ones that you don’t need.

Note: You can also do a quick merge via Address Book itself from dupli­cate con­tacts. How­ev­er, you won’t be able to see which con­tacts are being merged as its just a set-it-and-for­get kind of deal. If you do not care and sim­ply just want to get rid of those dupes, you can do so by doing the fol­low­ing:

  1. Launch Address Book
  2. Select Card from its menu
  3. Then, select Look­ing for Dupli­cates… and just go through the fol­low­ing prompts after search results fin­ish­es

Detect Operating System with JavaScript

Here’s a snip­pet of JS code that might come in handy if you, let’s say, would like to fil­ter out what type of con­tent to dis­play depend­ing on your web­site user’s OS. A real-life exam­ple would be hav­ing a down­load link show up to user’s who have a client that was only pub­lished on Win­dows.

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

Note: An alter­nate way can be found via PPK’s quirksmode.org