How to get the moderator role in bbPress

I don’t work with bbPress that much, but when I do – every time I spend most of my time hunting for functions. I wish there was some sort of documentation developed over the years, but it hasn’t, and I know, I know – I haven’t helped either. So at the very least, I decided to write down some of the common things I do with bbPress, starting with getting the moderator role. More

Named CSS Grid areas with Stylus

I recently started learning the new CSS Grids, and I love it! If you don’t know what CSS Grids are yet, you have to watch this talk by Morten Rand Hendriksen and this one by Rachel Andrew. They’re great, and I found them to be enough to start experimenting with grids.
More

Download a complete single page with wget

A simple way to download a complete page

wget -NmkEpnp -e robots=off [URL]

Very much inspired by Guy Rutenberg,
I only modified the snippet slightly with -N, which validates timestamps and doesn’t download duplicated ( but does overwrite local files with the new changes ) and robots=off, so I wouldn’t download the robots.txt

A simple one-liner to convert all JavaScript to CoffeeScript

A simple one-liner to convert all JavaScript in a folder to CoffeeScript

First, make sure you have installed “js2coffee” ( and js2coffee.org is real cool for single files too ):

npm install js2coffee

And then just paste this:

for f in *.js; do js2coffee -it $f > ${f%%.*}.coffee; rm $f; done;

`-it` options modifies the spaces to tabs, remove it if you prefer spaces.

Enjoy!

CoffeeScript Blows. Minds.

Was looking for a simple way to write an alternative of PHP’s in_array() function in JavaScript, and since I’m using actually writing CoffeeScript, decided to look there as well. And sure enough. Mind Blown.

 

        for val,key in values
            if val in selected_values
                return "MIND BLOWN."

Turns into

var key, val, _i, _len,
  __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

for (key = _i = 0, _len = values.length; _i < _len; key = ++_i) {
  val = values[key];
  if (__indexOf.call(selected_values, val) >= 0) {
    return "MIND BLOWN.";
  }
}

 

 

 

How to clean an URL with Regex

A regex pattern to fix 1st level domains. Useful for when data is submitted from users:

Turns this:

google.com
www.google.com
google.com/
http://google.com
http://www.google.com
http://www.google.com/

Into This:

http://www.google.com
http://www.google.com
http://www.google.com
http://www.google.com
http://www.google.com
http://www.google.com

Expression:

Search: 
((http://)?(www.)?([wd-]*?.)(w{2,4})/?)
Replace:
http://www.$4$5

PHP Usage:

	# Fix the URL:
	$fixedURL = preg_replace('/((http://)?(www.)?([wd-]*?.)(w{2,4})/?)/i', 'http://www.$4$5', $previousURL);

Danish ZIP Codes (PHP Array)

Just in case someone needs this some day.

Usage: [0] => Region Title, [1] => Min ZIP Number, [2] => Max ZIP Number

$denmark = array(
array('København', 1000, 1799),
array('Frederiksberg', 1800, 2999),
array('Nordsjælland', 3000, 3699),
array('Bornholm', 3700, 3799),
array('Færøerne', 3800, 3899),
array('Grønland', 3900, 3999),
array('Øvrige Sjælland', 4000, 4999),
array('Fyn', 5000, 5999),
array('Sønderjylland', 6000, 6999),
array('Vestjylland', 7000, 7999),
array('Østjylland og Midtjylland', 8000, 8999),
array('Nordjylland', 9000, 9999)
);

Trouble replacing an ampersand ( & ) in PHP with str_replace – Solution

Now this took about a half an hour until I figured this out. Posting this, just in case someone finds it – I hope I save you 30 minutes.
I fetched a link from HTML content with preg_match(), and I had to replace some data in that link.

str_replace("upgrade-plugin&plugin",'update-selected&plugins', $updateItem);

For some reason – the ampersand ( & ) wasn’t found and so the string wasn’t replacing it. At the end I figured it’s the ampersand problem, and here is the fix (sometimes the simplest things…)

str_replace("upgrade-plugin&plugin",'update-selected&plugins', $updateItem);

Update:
I had to use this method, because the ampersand was extracted from HTML document with preg_match. To avoid issues like this – use

html_entity_decode()