Mastodon Autopost Plugin for WordPress

There are several plugins for WordPress that allow to automatically create a new Mastodon toot (what a stupid name!) for each blog post. I’m using “Mastodon Autopost“. Unfortunately this plugin seems to be outdated and also has a bug that with the current PHP version results in an error/warning message if the post does not have any tags. I was able to fix it though and that fix is amazingly simple.

In line 545 of mastodon_autopost.php there is an if statement using sizeOf() which bombs out because the parameter $post_tags is not an array. Since I am far from being a PHP expert I had to first google that command and found that it isn’t necessary in this case since the foreach that follows later takes care of empty arrays. So the fix looks like this:

    $post_tags = get_the_tags($id);
    // <==== check was here
    if ($post_tags) {
        foreach ($post_tags as $tag) {
            $post_tags_content = $post_tags_content . '#' . preg_replace('/\s+/', '', html_entity_decode($tag->name, ENT_COMPAT, 'UTF-8')) . ' ';
        }
        $post_tags_content = trim($post_tags_content);
    }
    $message_template = str_replace("[tags]", $post_tags_content, $message_template);

If you can see this post on Mastodon, the fix works.