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.

Posted by & filed under Code Tips.

For a site I’m working on I needed a way to intercept a comment and block if a specific condition wasn’t met. This is what I wound up with.

function preprocess_comment_handler($comment_data) {
        if (/* Some condition if they can comment */) {
            return $comment_data; 
        wp_die('An error message that they cannot comment'); 
add_filter( 'preprocess_comment' , 'preprocess_comment_handler', 1 );

This hooks into the preprocess_comment filter which normally lets you edit comment data before it’s submitted to the database. In this case we’re not doing anything to the comment data if the comment is allowed to pass through, but we’re just killing the process if they’re not allowed to leave a comment.

One thing that I might go back and do later is redirect to an error page rather than just using wp_die. But for now, this is working well enough.

Posted by & filed under Code Tips.

private String rotateString(String s, int distance) {
    String out = "";
    for (int i=0; i<s.length(); i++) {
        out += s.charAt((i + distance) % s.length()); 
    return out; 

This comes from needing a quick and easy way to rotate a String in Java.

Posted by & filed under Code Tips.

Needed a way to count how many quiz attempts students had on a specific quiz over a certain grade. The query below works with the Ad-Hoc database query Moodle plugin and lets you enter a module ID and what grade you want to count for a minimum.

SELECT u.lastname, u.firstname, u.idnumber,
	FROM prefix_quiz_attempts attempts
	JOIN prefix_quiz quiz
	JOIN prefix_course_modules cm
	AND state='finished'
	AND (attempts.sumgrades / quiz.sumgrades * quiz.grade) >= :minimum_grade
) AS quizcount
FROM prefix_user u
HAVING quizcount>0
ORDER BY u.lastname ASC, u.firstname ASC