jQuery: Get the text value of a node without it’s children

Use the following code to get the text value of a node without the text content of it’s children (unlike jQuery’s text() method):

var node = $('#selector'),
    text = $.trim(node[0].childNodes[0].nodeValue);

var hastext = text.length > 0;

git: show and change URL of remote origin

To show the current remote origin URL, use:

$ git config --get remote.origin.url

To change it, use:

$ git config remote.origin.url [new-url]

git: revert changes made to working copy

To revert changes that you have made to your local working copy, but you have not yet commited (like svn revert), use:

git checkout .

To revert a single file:

git checkout path/to/file

To remove unversioned files:

git clean -f

OS X Terminal: Open current directory in the Finder

All you need to do to reveal a directory/folder in the finder from the terminal is to type:

open .

That's it!

Clear the cache in Chrome mobile on iOS

From the “Chrome” menu (three horizontal bars), select Settings, then Privacy > and then click Clear Cache.

Mac OS X: Disable autocorrect in commit messages

Mac OS X automatically “corrects” what it deems to be spelling mistakes in commit messages in source control applications such as Versions, Tower, GitHub, etc. Because commit messages often contain non-standard text, autocorrect often changes legitimate input, for example dist becomes diet.

To disable autocorrect, simply right-click in the commit message text area, and under “Spelling and Grammar” disable “Correct Spelling Automatically”.

Finding large directories through the command line

A quick way of showing which directories are taking up how much space on unix systems.

Starting at the root, use du to show disk usage.

cd /
du -h --max-depth=1

-h makes the output human-readable, and -max-depth=1 stops du form descending into subdirectories.

You should get output that looks something like this:

6.2M    ./sbin
8.0K    ./home
232M    ./opt
4.0K    ./boot
4.0K    ./mnt
5.2M    ./bin
8.0G    ./var
0   ./sys
4.0K    ./media
80M ./root
6.7M    ./tmp
1.3G    ./usr
120K    ./build
4.0K    ./srv
12K ./dev
8.9M    ./etc

var and to a lesser extent usr are hogging disk space. Then just cd into var and run du -h --max-depth=1 again to see which subdirectories are largest. Repeat.

Bash Terminal: Show all available aliases

Aliases defined in .profile or .bash_profile are useful shortcuts for everyday tasks in the terminal.

To list all defined aliases, simply type alias.

$ alias
[list of aliases]

Show line numbers in vi

To show lines numbers in vi, use the following command:

:set nu

To hide them again:

:set nu!

The commands only affect the current document. To always show line numbers in vi you have to add the following line to ~/.vimrc:

set nu

jQuery: textarea content losing line breaks on update.

If you’re updating a textarea’s content using jQuery, you might understandably use the text(var) method, thinking that you are updating the text content of an HTML element.

However, on Internet Explorer at least, you’ll lose the line breaks, although they are present in var.

To preserve the line breaks, use the val(var) method to update the textarea – jQuery realises that the HTML element is a textarea, not an input, and updates the content accordingly.

MySQL connection details/credentials for ClearDB on AppFog

AppFog offers ClearDB as a simple-to-install plugin. One click in the AppFog admin interface creates a “aluminium” account at AppFog and sets up the database.

The connection details are made available to your app in the CLEARDB_DATABASE_URL env variable.

It is also possible to use a MySQL client (e.g. the mysql CLI, or Sequel Pro on OS X) to connect directly to ClearDB. All the required info is available in the Env Variables tab on the AppFog dashboard, where the CLEARDB_DATABASE_URL variable is listed. It has the following format:

mysql://[user]:[pwd]@[host]/[db]

MySQL: Count email addresses by domain

If you have a table (e.g. my_table) with an email address column (email), and want to count how many addresses are in the table for each domain, you can use the following query:

SELECT COUNT(*) AS count,
    SUBSTRING_INDEX(email, '@', -1) AS domain
    FROM my_table
    GROUP BY domain ORDER BY count DESC

GMail div class=”im” results in purple text.

Problem

When testing HTML emails you might notice that GMail shows HTML text in purple, overriding the CSS styles you defined in the newsletter HTML.

This is because you probably sent the same (slightly altered) email to yourself more than once, resulting in a thread of several emails. GMail, being clever, assumes that because a text passage appears in all emails, it is being quoted in the following emails. It wraps the text in a div class="im" which displays the text in purple.

Solution

Delete all previous emails, and the text in the email should be displayed as you intended.

Show recently modified files on Unix

The following command will list all files and directories within the current directory that have been modified in the last day:

find . -mtime -1 -ls

Use -mmin to use minutes instead of days. This command only lists files within the current directory and any subdirectories that have been changed in the last five minutes:

find . -type f -mmin -5 -ls

Multiple WordPress blogs using categories

Often a single website (or WordPress installation) will be used to cover two distinct topics. Topic A might be relevant to one group of readers, and topic B to another. We don’t want to bore the original subscribers of topic A with the content of topic B. The sensible solution would be to create a “secondary” blog for topic B.

However, the standard WordPress installation doesn’t allow us to create multiple blogs – the official solution is to upload another WordPress installation to the server in a parallel directory: example.com/topic-a/ and example.com/topic-b/ both being separate WordPress installations with separate configs, databases, etc., with all the maintenance problems that come with that.

A better way to do it, with one install, is to use a separate category for the “secondary” blog.

Create a top level category for the secondary blog

Create the category in WordPress, and make a note of the ID. (Let’s say it’s 123.)

Exclude the category from the home page

Create a functions.php file in the base directory of your theme (if you don’t already have one), and add the following code:

<?php
function exclude_category_from_home($query) {
    if ($query->is_home) {
        $query->set('cat', '-123');
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_category_from_home');

(Don’t forget to replace 123 with your own category ID!)

This filter simply removes the category ID from the database query.

Exclude from the “default” category list

If you have a category list in the sidebar (or anywhere else), you probably don’t want the “secondary” blog category to show up there. Easy enough using the wp_list_categories option exclude (documentation).

<ul>
<?php wp_list_categories('orderby=name&exclude=123'); ?> 
</ul>

You can then show a link to the secondary blog using the include option:

<ul>
<?php wp_list_categories('orderby=name&include=123'); ?> 
</ul>

Exclude the category from the RSS feed

Excluding a category from the WordPress RSS feed is relatively simple.

If the standard WordPress RSS feed URL is as follows:

http://www.example.com/feed/

the feed without our category is:

http://www.greatwhiteark.com/feed/?cat=-123

I would recommend piping the feed through a service such as Feedburner. Users subscribe to the Feedburner URL, which allows us to change the actual feed URL without losing any subscribers.

If you’re not using something like Feedburner, you can use Apache’s mod_rewrite to redirect the old feed URL to the new one.

Offer a feed for the secondary blog

The feed URL for our new blog is simply the category feed:

http://www.greatwhiteark.com/feed/?cat=123

Create a custom single template for the new category

Rename your single.php template single-default.php.

Create a template for the new category and call it single-123.php.

Create a new single.php file with the following code:

<?php
$categories = get_the_category();
$tpath = TEMPLATEPATH.'/single-'.$categories[0]->cat_ID.'.php';
if (file_exists($tpath)) {
    include($tpath);
} else {
    include(TEMPLATEPATH.'/single-default.php');
}

Passing variables to the WordPress header template

Sometimes you want to pass a variable from the template to the header, for example a $bodyclass variable to set a custom class attribute on the body element.

You might think that a variable set in the template file is available in the header included using the WordPress function get_header(), but it isn’t.

<!-- The template file -->
<?php
    $bodyclass = 'wide';
    get_header();
?>

<!-- header.php -->
<body
<?php
    // WARNING: $bodyclass is not set.
    if (isset($bodyclass)) {
       echo ' class="'.$bodyclass.'"';
    }
?>
>

The get_header() function does not inherit the scope of the template calling it, so the $bodyclass variable is lost.

In order to maintain scope, just include the header template directly:

<!-- The template file -->
<?php
    $bodyclass = 'wide';
    include('header.php'); // assumes in same dir as template file
?>

<!-- header.php -->
<body
<?php
    // $bodyclass is now set in header.
    if (isset($bodyclass)) {
       echo ' class="'.$bodyclass.'"';
    }
?>
>