How not to program

Today, I tried to write some simple code to use #-tags in the wordpress backend. It should recognize the #tags, remove the #, add the tags to the post if they’re not present yet and, if they don’t yet exists, create those terms.
I had to dig through a lot of wordpress API documentation, Stack Overflow answers, the PHP manual for preg_replace and preg_match_all, and some simple functions I always forget the name of like strtolower or substr (I am blessed with a weak memory).

First, the code:

function ccb_quicktagsfilter( $content ) {
global $tag_ids;
$pattern = '/#(\w+)/i';
$replacement = '${1}';
preg_match_all($pattern, $content, $matches);

foreach ($matches[1] as $term) {
if (!($tid = term_exists( $term, ‘post_tag’ ))) {
$t = wp_insert_term( $term, ‘post_tag’, array(‘name’=>$term) );
$tag_ids[] = $t[‘term_id’];
}
else {
$id = get_term_by( ‘name’, $term, ‘post_tag’ );
//$id = term_exists($term);
$tag_ids[] = intval( $tid[‘term_id’]);
}
}

return preg_replace($pattern, $replacement, $content );
}
function ccb_quicktagsaction($postid) {
global $tag_ids;
wp_set_object_terms( $postid, array_unique($tag_ids), ‘post_tag’, true); //append tags
}
add_filter( ‘content_save_pre’ , ‘ccb_quicktagsfilter’,10, 1);

add_action(‘save_post’,’ccb_quicktagsaction’);

It took me a long time to get it right (and there might still be mistakes). Everybody makes mistakes, but needing hours for some simple line of code is pathetic, especially if you have a Master’s degree in computer science (sic!). I shouldn’t be doing this, I know. I want to contribute something positive to the world, I have many ideas to cultivate and a lovely daughter to raise.

So if this sounds recognizable, read on. This is for people who cannot code (or cannot code except for doing trial-and-error, which is the same).

So how did I do it? I had this idea of doing tagging in WordPress with a ‘#’ and adding the tags automatically to the database using WordPress hooks and filters. I started to search for a filter and found content_save_pre. This was one part: I needed to remove the #. This could be done with preg_replace, so I looked at the PHP manual for that function. It had many examples and comments, a lot of which was very complex and made me feel like I’m a stupid idiot and don’t deserve to live.

Anyway, I read and started to experiment with the regexps. It was not until I tried using single quotes that I got it working. The #s got stripped away using the most simple regexp

#(\w+)

. I had to figure out, by trial and error, that \w matches a word-character and not a whole word. I had to figure out that the returned $matches is an multidimensional array, and the standard flag is set so that we need the second subarray, that matches the ()-section rather than the whole thing.

The # were gone, and I had an array with my new terms. I looked at the WordPress documentation for the functions to add these tags. wp_insert_term if the tag didn’t exist yet, otherwise just mark the tag id. But I couldn’t access the $post-ID from within the filter. I googled the issue and lo and behold, there was a thread and a plugin. Someone wrote a plugin to make the postid available where it was designed not to be available.

I was lost, then thought about using a global array. Where to declare it as global? Does it work at all? Let’s test with some dummy value. Change it back. Etc.

There is an action hook save_post that should be used for this. It’s easy to write a function using wp_set_object_terms and the global tag array that was created in the filter function (that we can assume is always executed before the action hook).

Now it was ready for testing. But instead of tags the numbers showed up. It seemed like the tag id’s were converted to the slugs and names and there were bugs in WordPress core and I had to program everything from scratch and I am completely worthless. I tested again and again, refreshed browser caches and manually edited tags (this all gobbles up time). Eventually I read *somewhere* that WordPress interprets numbers as strings.

So what happened? The tags were added (after I figured out the return value for term_exists) as numbers. The $tag_ids-array should have integers as keys and the names of the tags (not the slugs – another 15 mins) as values. But when the function added the id as a string, PHP just added it to the associative array, something like ‘642’ -> null. Now WordPress was kind enough to interpret that value as a new tag with auto increment number 687, and name/slug “642”, when it didn’t ignore the string-index.

This kind of bullshit takes a long time. The particularities of WordPress and PHP are annoying, there is always something going on behind your back. Yes, I know, that is why people go for RoR or MEAN or any other clean stack.

I solved the thing by converting the value to an integer. In the process, my editor complained about the ‘${1}’ (took me a while to figure out why). How many lines of code? Just a few. Feeling like a complete moron, not worthy of anything. That sounds like a typical addiction symptom. But let’s not complain about that here, I know what I have to work on and that’s none of your business. I wanted to write about the process of coding, and how not to do it. And this is a hands-on example. Don’t do trial and error. Don’t get lost in lengthy documentation. Don’t use badly documented functions. Don’t type before you have solved the problem conceptually. Don’t fear the machine. Don’t stare at the screen. Don’t write makeshift shaggy “test” code.

I work with NotePad++, I don’t have a dedicated editor, and trial and error is the method that works fastest. But this is ridiculous. But unfortunately, it can become an addiction. When I really want to solve these little problems, like always, I bite. This biting at every bone is in my genes – I’m a mad dog.

Eventually, it seemed to work. I turned it into a plugin and will upload it to the WP repository. A piece of code that should take a seasoned programmer no longer than 20 mins to research and 10 mins to code, but took (away from) me valuable hours, in which I should have written poetry, studied anticapitalism and permaculture, hugged my wife, and do yoga.

That is how not to program.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.