Posted by & filed under WordPress.

I’m using bbPress on a couple of support forums. But a few days ago I went to share a thread on Facebook and noticed that the description coming from WordPress with Yoast SEO installed is showing the first part of the page content. In my case it was something like “This thread contains 2 voice, 3 posts” which is not what I want.

What I wanted was the text of the original post.

With a little filtering it wasn’t too tough.

I put this in my functions.php file since it was theme specific, but could be a really simple plugin as well.

Now it pulls the text from the original post and puts it into the meta description along with the open graph description fields.

Posted by & filed under Coding.

Earlier this week I was working on a JUnit test for one of the AP free response questions from 2016 where you’re given an array of Strings and need to implement a class that randomly pulls one out using a getNext method. You’re basically writing an iterator without calling it that.

But since it was random I needed a way to check that the right values were there, even though they probably weren’t going to be in the same order. So I needed to check that one String array was a permutation of another. And I really didn’t want to have to go through every permutation of one to see if one matched. That seemed horribly inefficient.

Google brought me to this page of interview questions with a cool solution for int arrays. The TL;DR version is that you get the sum and product of both arrays, and if they match they’re permutations of each other.

And here’s a bit of Java code to do that.

First check is to make sure the two arrays are the same length. If they’re not, there’s no reason to go on. Then it’s just a loop to get all the values and do a bit of math.

But what about Strings

That was close, but I needed to do the same with a String array. Enter hashCode.

And it worked, at least for the short arrays I threw at it. I assume it’d fail with either long strings or longer arrays since eventually multiplying a bunch of ints together is going to overflow a long. But it worked well enough for what I needed.

Posted by & filed under Computers & Internet.

A few months ago I bought an Epson DS-510 scanner to try and keep up with the ever growing mound of paper that you deal with. And overall it’s been a great scanner. It’s fast. Scans duplex. And the scans look good. But there’s been one nagging issue and that’s a Cannot Communicate with the Scanner error message.

If my computer went to sleep or I unplugged the scanner to move my notebook, the next time I plugged it in I’d get the dreaded Cannot Communicate with the Scanner message and would be unable to scan until I restarted my computer.

 

Cannot Communicate with the scanner Message

I tried all the fixes that Google found for me. Swapped out cables. Tried different ports. Reinstalled drivers. Nothing seemed to work.

Then I came across a site that had the fix. And I’d love to link you there, but I forgot to bookmark it.

And it’s a pretty easy fix. What looks like was happening is that the Windows Image Acquisition (WIA) service was hanging up. In the services list it showed as paused, which was a little weird.

The Fix

So, on to the fix. First go to the Task Manager. Easiest way to get there is press Ctrl-Alt-Delete and pick Task Manager from the menu that comes up.

Find the Windows Image Acquisition (WIA) process and select End Process. You’ll get a nasty warning from Windows. I’ve just ignored it and haven’t had any issues. Can’t promise you’ll have the same luck.

Windows Task Manager with Windows Imaging Acquisition service selected

And, you may be able to skip ending the task. But I had to do this for the next step to work.

Now you want to go to the Services list. Easiest way to get there is bring up the start menu and start typing Services. You can also get there through the Administrative Tools.

Once there you’re looking for the same Windows Image Acquisition service that you just ended. It’ll probably show Stopped. (It shows running here because I had already restarted it)

Windows Services List

Right click and select Start.

And, assuming it comes back up, that’s it. I’ve done this a dozen or more times by now and it’s worked every time.

Posted by & filed under Coding.

Working on a small weekend project I needed an easy way to validate that a string contained a valid Twitter username. Not necessarily an active account, but that could be an account.

Found the rules on Twitter’s site that a username can be between 1 and 15 characters and can contain only the characters A-Z, a-z, 0-9, or an underscore. Sounds like a perfect use for regex. So here it is, both in JavaScript and PHP since I needed both.

Posted by & filed under Code Tips.

For a Moodle plugin I’m writing I needed a way to check if a String exists. Normally, if a string doesn’t exist then Moodle just outputs [[missing_string]] (replace missing_string with whatever you’re calling the string) and moves on. But, in developer or debug mode it also displays a stack trace.

What I needed was a way to check if a string was set, and if not display a different string which I knew was set. This is what I came up with.

function get_string_check($identifier) {
	if (!get_string_manager()->string_exists($identifier, 'plugin_name')) {
		return get_string('a_known_string', 'plugin_name');
	}
	return get_string($identifier, 'plugin_name'); 
}

The reason I needed this was that I was building strings based on error constants. So there might be a string err_1, err_2, err_3, etc. But I needed a way to not display an error if I requested err_99 without there actually being that string.

The only catch is that the you must have $string['a_known_string'], or whatever you want to call it, in the language file for your plugin. For me, it just says Unexpected Error.

Posted by & filed under Code Tips.

Not really code, but still something I need often enough to write it down.

This command will list all files in a Linux folder sorted by date with the most recently updated files at the bottom.

ls -ltrh

From the ls man page, here’s what each of the choices do.

-l – Uses a Long format which shows more information on each file

-t – Sorts by modification time

-r – Reverse order. Without this the most recently updated files would be at the top of the list instead of the bottom.

-h – Human friendly file sizes. Shows files in KB, MB, or GB instead of just bytes.

Posted by & filed under Code Tips.

This snippet will let you run the same command using each line from a file as a parameter.

cat filename.txt | while read in; do echo "$in"; done

As written it’ll open up filename.txt and echo out every line. Probably not what you want, so replace echo “$in” with whatever command you want to run. The $in variable is the line it’s pulling out of the file.

What I needed this for was to block a bunch of IP addresses that were hitting honeypot files on a server so instead of echo I was running csf to add the IP.

Posted by & filed under Code Tips.

This little batch file came about from needing to run a cron job from within XAMPP. Since it’s Windows I could have used a scheduled task, but I don’t have XAMPP running all the time and I wanted a little more control over when it runs.

And on this specific project, I’m working on a Moodle plugin that needs to run under cron every minute. So the program that’s running is PHP calling the Moodle CLI cron.php file. You’re probably going to want to change the script that’s called.

:loop
php /path/to/your/script.php
@timeout /t 60
@goto loop

You’ll want to change the 2nd line to whatever you want to run. Add as many lines as you want. And then change the 60 on the 3rd line to however many seconds you want it to wait. I needed it to run every minute so I’m using 60 seconds.

Now when I need to have cron active I just drop to the command line and run this batch file and leave it running until I’m done.

And for those that haven’t dealt with the command line in a while, or ever, pressing Ctrl-C will terminate the batch file.

Posted by & filed under Code Tips.

Fighting against an issue with the sendmail queue getting filled with junk I needed a way to delete a folder full of messages, and there were too many for rm to delete.

A post on Stack Overflow had the solution. Only thing I added was the -v option to rm so that it displays the files as they’re being deleted just to double check that something was actually happening.

for f in *.pdf; do rm -v "$f"; done