How to fix broken images in WordPress?

Image Not Found

Nothing exists forever — including those images that you inserted in your blogposts from various external sources. Those who have been blogging for a long time now know the pain of such broken image links. Due to corrupt files, incorrect links, expired domains, deleted resources or whatever — your WordPress site can occasionally yield 404-errors on images — leading to negative SEO and poor reading experience.

One solution that exists — is to replace those image links with proper ones — which is a big headache.

But there exists a simple Javascript snippet which you can add to your site’s header and replace all broken images with a default one – in a jiffy.

Let’s assume that the image that you want to use as replacement of broken images is located at /wp-content/uploads/2018/08/replace.png, you can add this Javascript to your WordPress blog’s header:

$('img').error(function(){
$(this).attr('src', '/wp-content/uploads/2018/08/replace.png');
});

If you don’t know how to do so, you can use a plugin like Header and Footer Scripts to insert your custom javascript arguments.

Alternatively, you can just place this PHP code snippet in your theme’s functions.php file, anywhere between <?php and ?> :

function imgReplace() { ?>
<script>
  $('img').error(function(){
$(this).attr('src', '/wp-content/uploads/2018/08/replace.png');
});
</script>
<?php }
add_action( 'wp_head', 'imgReplace' );

This is a totally safe practice and won’t do any harm if your site has no broken images at all.

Total
0
Shares


Feel free to ask questions, send feedback and even point out mistakes. Great conversations start with just a single word. How to write better comments?
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.

You May Also Like

How to Make Your Blog Ready for 2019?

Times are changing and for your blog to rock in 2019 you need to change along with them. Today it’s not about the length of your posts but their format. You also need to accept that social media and smart devices are dominating the world. Therefore, your blog has to leverage their power to reach a wider audience. 3 Tips…

10 Ideas to Make WordPress Blogs SEO Friendly

If you are starting a blog then WordPress can be an exciting platform because it’s free, user-friendly and has a variety of plugins that give you many options. It is also known to be a powerful content management system, which is an added advantage. Moreover business owners and individuals alike can benefit from it because it’s very simple to optimize…

Use These SEO Tips To Boost Your Website Rankings

Do you want more traffic visiting your website? If so, then some search engine optimization is what you probably need. At first glance, this can seem pretty random; however, there are specific algorithms that look for particular elements in determining website rankings. Keep reading to learn a number of SEO tips you can use to enhance how much visibility your…

How to Publish and Proofread? : Awesome Blogging tools for Windows

I am an experimental Windows user. I find, install and experiment several new applications on my Windows PC every week. Most of the applications and tools, I have installed are directly or indirectly about  desktop publishing. Some of them are aimed to help me with my study and some others are for web development . In addition to them, there are…

YouTube Video search : Tips and Tricks

When it comes to online video viewing, nothing beats YouTube. It has world’s largest video collection of all genres possible. Without a doubt, YouTube is so essential to video lovers, as Google is to search and Wikipedia is to research. Started in 2005 by three ex-PayPal employees, YouTube was bought by Google for a whopping $1.65 billion and still runs under Google‘s…