Fix ALT tags for avatars in BBPress (WordPress)

I recently discovered a little “error” on one of my websites running WordPress with BBPress.
The ALT tag for all avatars/gravatars used in my WordPress BBPress forum was set to: alt=””.

Bing, according to their webmaster tools, did complain over the lack of the BBPress avatars ALT tags on most pages in my forum.
Seeing all those error messages annoyed me too much, so I decided to fix it.

BBPress forum ALT tag fix.

So, here how to fix the problem:

You need to edit your themes functions.php file.
Add this bit of PHP code to the bottom of that file:

function filter_gravatar_alt($img) {
   if ( have_comments() ) {
      $alt = 'Avatar image';
   }
   else {
      $alt = 'Avatar image';
   }
   $img = str_replace('alt=\'\'', 'alt=\''.$alt.'\'', $img);
   return $img;
}
add_filter('get_avatar', 'filter_gravatar_alt');

Save the functions.php file and upload it or just update it if you’re using WordPress built-in editor.

Hopefully you’ll see that your BBPress avatars/gravatars contain the ALT tag with the text “Avatar image”.

Auto add ALT to WordPress featured images

I found this bit of PHP code that worked fine. What it does is that if your featured images in WordPress have empty ALT tags, then it will use the WordPress post title to fill them.

Add the following code to your theme functions.php file. And not the WordPress functions.php file…

function lwp_37481_featured_alt_text($metadata, $object_id, $meta_key, $single) {
    if(isset($meta_key) && $meta_key == '_wp_attachment_image_alt'
        && get_post_thumbnail_id() == $object_id
        && $single === true
    ){
        $original_value = get_post_meta(
            $object_id, '_wp_attachment_image_alt', false
        );
        if(empty($original_value)){
            return get_the_title();
        }
    }
    return $metadata;
}
add_filter('get_post_metadata', 'lwp_37481_featured_alt_text', 10, 4);

I got the code from this guy: https://letswp.io/post-title-alt-text-featured-image/

Updated WordPress Blog Ping List (2021)

Have you seen a gigantic list of blog ping URLs or is currently using such a huge list of blog ping services?
I’m talking about close to hundreds of URLs.

Take a minute to look through them. I bet 90% probably are probably not working.

It will only take more unnecessary time for you to post your blog posts and it will also use more resources from your web server.

The screenshot below show my old WordPress ping list, there’s like 80 more blog ping services except the ones you see in the screenshot.

Too many old blog ping services.

You don’t gain much if you’re using the huge lists many other sites got.
They’re not updated anyway and contain a lot of 404 sites.

Instead, just use a few blog ping services that actually works in 2021. The following is a updated list of 8 ping sites.

Here’s the Blog Ping List for 2021 I currently use.

http://rpc.pingomatic.com
https://rpc.twingly.com/
http://blogmatcher.com/u.php
http://ping.feedburner.com
http://ping.blo.gs/
http://www.blogdigger.com/RPC2
http://pingoat.com/goat/RPC2
https://www.pingmyblog.com/ 

That’s all for now. 🙂

Direct link that will delete a WordPress post (PHP)

This is a bit of very badly written PHP code that will add a link under each post.
When clicking on “Delete Post” the post will instantly get deleted.

Only when you’re logged in as a administrator you’ll see the link.
Add this to your themes index.php file somewhere inside the loop.

if (current_user_can('administrator')) {
	
    global $post;
	$thepostid = $post->ID;
	echo "<center>The Post ID: " . $thepostid . " ";
	$thepostdeletelink = get_delete_post_link ( $id = $thepostid, $deprecated = '', $force_delete = false );

	echo "<a href=\"$thepostdeletelink\" target=\"iframedelete\">Delete</a></center>";
	echo '<iframe src="#" name="iframedelete"  width="2" height="2" scrolling="no"></iframe><br>';
}

Use it carefully since when clicked, it deletes the post instantly. That’s all.

Fix: WordPress Sitemap Error: “XML declaration allowed only…”

Have you seen a error similar to the one below while trying to access your WordPress Sitemap?

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

Wordpress sitemap.xml error

I’ve looked around and one guy solved the problem by removing some PHP-code that indicates the PHP code ending or whatever you call it “?>” in his themes functions.php file.

Because apparently he had 2 of them which caused the sitemap error for him and someone else too in that thread.

But that was not the problem for me. The problem was because some idiot (me) had made a plugin and left linebreaks and spaces in the plugin PHP-file.

So check your WordPress plugins. Deactivate one at a time and hit F5.

Have fun! 😉