Posted by & filed under Code Tips.

Found this one on Stack Overflow needing a quick and easy way to create somewhat random strings.

function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var randomString = '';
    for (var i = 0; i < len; i++) {
    	var randomPoz = Math.floor(Math.random() * charSet.length);
    	randomString += charSet.substring(randomPoz,randomPoz+1);
    return randomString;

If you visit the entire thread where this code was posted there’s a pretty good discussion of how to create random strings using math and date functions.

Posted by & filed under Code Tips.

As a follow up to an earlier post on double braces in Java, came up with a short, but ugly way of filling an ArrayList via an array.

String[] ray = {"Hi", "there", "Bob"};
ArrayList<String> lst = new ArrayList<String>() {{
	for (String s: ray) { add(s); }

Of course, at that point you might as well just do this.

ArrayList<String> lst = Arrays.asList(new String[]{"Hi", "there", "Bob"});

Posted by & filed under Code Tips.

This is one of those that I never remember, but use a lot whenever I want to pull an entire file into a String quickly.

String text = new Scanner(new File("file.txt")).useDelimiter("\\A").next();

\\A is the start of the file, so Scanner is going to try and split on the beginning of the file. Since there’s only one beginning, there’s only one next.

Posted by & filed under Code Tips.

Not sure why, but it looks like sorting on last name and first name is not built into the WordPress get_users method. Seems like it would be a pretty basic need, but it’s not there. So, it’s on to PHP to make it happen.

This bit of code assumes that $user_list has already been filled with users by calling the get_users method.

usort($all_users, function($a, $b) {
	return strnatcmp($a->last_name . ', ' . $a->first_name, $b->last_name . ', ' . $b->first_name);

Once it’s done, $user_list will be sorted based on lastname and firstname. It’s actually sorting on “lastname, firstname” which should work out the same.

Posted by & filed under Code Tips.

For a project I’ve been working on I needed to use preg_match quite a bit. But I got really tired of having to punch in several lines, so I cooked up this little snippet to pull out a specific match group.

 * Shortcut to doing regex without needing to worry about $matches
 * @param type $pattern
 * @param type $search
 * @param type $group
function preg_search($pattern, $search, $group=1) {
    if (preg_match($pattern, $search, $matches)) {
        if (isset($matches[$group])) {
            return $matches[$group];
        return false; 
    return false; 

$pattern and $search work just like normal preg_match. The $group parameter tells it which matching group to return, if it exists. If it doesn’t exist, it returns false.

Posted by & filed under Photography.

Posted by & filed under Code Tips.

Came across this the other day on Stack Overflow. Turns out there is a way to one line create an ArrayList and fill it all at the same time.

ArrayList<Integer> lst = new ArrayList<Integer>() {{

It looks odd, mostly because that’s the first time I’d come across it. But it’s the same as doing this.

ArrayList<Integer> lst = new ArrayList<>();

One slight technical difference. The double braces creates an anonymous inner class to fill. Not sure if what cases that would matter though.

Also played a bit today and found that you have to specify the data type on both sides of the declaration. Feels weird to put Integer in both sets of <>.

Posted by & filed under Code Tips.

Quick little snippet to redirect your visitors to another page after they leave a comment.

function comment_redirect($location, $comment) {
	return site_url('/thanks/'); 
add_filter('comment_post_redirect', 'comment_redirect');

Normally after leaving a comment in WordPress you’re sent back to the page where you left the comment. By using the comment_post_redirect filter you’ll be able to control where they go. As it’s written, this code will redirect back to a /thanks/ page on your site, but you could redirect it anywhere you want.

And a couple of things that are left out of most of the tutorials on this filter that I’ve seen.

First, whatever is returned from your function is where the visitor will be redirected. Of course, it’s a filter so it’s possible that another plugin will override your return value. If you absolutely have to be sure your code is the one that does the redirect you can either make sure your filter is the last in line or just call wp_redirect from the method.

And second, the $comment parameter contains information on the comment that was submitted. You can use this to make decisions on where to redirect. For example, I used this on a site where only comments on a specific custom post type should be redirected differently. One of the elements of the $comments parameter is the post that the comment belongs to. I was able to check the post_type for that post and determine what to do.

Posted by & filed under Code Tips.

This came about from needing a way to automatically post to a Pinterest board.

Unlike most social sites, Pinterest doesn’t have a public API to handle auto posting so it took a bit of finagling and a Node.js package called Pin-It Node which does most of the heavy lifting.

var PinIt = require('pin-it-node');
var pinIt = new PinIt({
    username: 'your_pinterest_username',
    password: 'your_pinterest_password'
var settings = {
    boardId: 'Numeric_board_id',
    url: process.argv[2],
    description: process.argv[3],
    media: process.argv[4]
}; , function(err, pinObj) {
    if (err) {

Once the script is in place, you’ll run it like this.

$ node pin-it.js "Pin Title" "This is a description for the pin" "http://example/image.jpg"

You will need the numeric ID for the board you’re posting to. You can either dig through the HTML source to find it or use a tool that I kicked up in an afternoon because I didn’t want to dig through the HTML more than once.

And originally I was looking for a PHP script that would do the same. But since I wasn’t able to find anything that worked, I wound up just running shell_exec to call this script from the PHP file.