One line preg_match for PHP

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. $pattern and $search work just like normal preg_match. The $group parameter tells it which… Read more »

Double brace initialization in Java

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. It looks odd, mostly because that’s the first time I’d come across it. But it’s the same as doing this. One slight technical difference. The double… Read more »

Include XML sitemap in robots.txt

Posted by & filed under Code Tips.

If you’ve got a robots.txt file and an XML sitemap, add the following line to robots.txt so that search engine spiders will pick up your sitemap without you having to submit. Of course, change the url to match your actual site.

Redirect after WordPress comment

Posted by & filed under Code Tips.

Quick little snippet to redirect your visitors to another page after they leave a comment. 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… Read more »

Automatically post to Pinterest

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. Once the script is in place,… Read more »

Blocking WordPress comments

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. 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… Read more »

Quiz count for Moodle ad-hoc reports

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.

Toggle checkbox with jQuery

Posted by & filed under Code Tips.

Quick little snippet to toggle the value of a checkbox with jQuery. 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… Read more »