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

Posted by & filed under Code Tips.

Quick little snippet to toggle the value of a checkbox with jQuery.

jQuery('#checkbox_id').prop('checked', !jQuery('#checkbox_id').prop('checked'));

I needed this a couple days ago using a span with a font awesome image to take the place of a checkbox. Still needed an actual checkbox underneath for the form handler to work with. So I used this little piece of code to toggle the state of a hidden checkbox and then used the state of that checkbox to determine what CSS class to use for the fake checkbox.

Posted by & filed under Programming.

Is XAMPP slow?

I’ve used XAMPP for pretty much as long as I’ve been doing web development. It makes it trivial to get Apache, MySQL, and PHP set up.Since upgrading from Windows 7 to Windows 10 though I’ve noticed it was running much slower. I just assumed that XAMPP is slow. There was also an upgrade to XAMPP as part of that, although I don’t remember what versions.

The slowness was especially noticeable working on Moodle plugins. It was almost unusable, although that’s not too surprising with the 250-300 database tables that Moodle uses.

Googling brought a bunch of potential solutions

  • Disable IPv6
  • Edit hosts file to point to localhost
  • Exclude Apache, MySQL, and PHP from firewall

And all of these seemed to help just a little bit, but so little that it could have just been psychological. Read more »

Posted by & filed under WordPress.

Always Edit in HTML Plugin ScreenshotThis is another one of those plugins that I’d thought about writing and Google kept me from needing to.

Sometimes when building a WordPress site I’ll need to create pages using straight HTML instead of the WYSIWYG editor. Without fail though I’ll accidentally click on the Visual tab and the editor will clear out most of my HTML I’ve thought that it would be nice to be able to disable the visual editor, but only for specific posts. Turns out, I wasn’t the only one.

The Always Edit in HTML WordPress plugin takes care of it.

Take a look at the screenshot on the right. Check the box and save your post and the visual editor is gone. Nothing to foul up the HTML.