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 into your site’s header and replace all broken images with a default one – in a jiffy.

[vcex_divider_dots color=”#dd3333″ margin_top=”10″ margin_bottom=”10″]

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
nomad
Read More

What to Write About on Your Travel Blog?

Do you run a travel blog but are at a loss of potential topics to write about? Maybe you’ve written about everything you think you can, from the best places to eat in different countries to sharing your personal adventures to tips on booking cheap flights and hotel rooms. Fortunately, there are there no shortage of topics to write about…
NewbloggingstylesthatyouneedtotryGauravTiwari
Read More

New blogging styles that you need to try

Creativity has no limits and rightly so it shouldn’t have. Writing, too, requires plenty of creative freedom. Writers, especially us – the bloggers, devise and apply ideas that are mostly unheard of. This allows us to tell what readers are looking on search engines for. It was 2013 and I was working on a college level paper project when I…
types
Read More

How to write a great product review?

Product Review blogging is one of the highest paying blogging methods being used by bloggers right now. Product reviews started as being a part of the blogpost but soon gained higher monetary values and emerged as a whole new writing strategy. Today it is no longer just another niche but has become the best way to feed and connect most…