Hello SEO Junkies, today I want to talk with you about the common SEMrush Site Audit Issue “resources are formatted as page link” which is asking you to remove WordPress Image links. We will talk about, what the issue actually means, if it is a real issue and how you can fix it.
What does “resources are formatted as page link” actually mean?
First things first, this isn’t a critical error. It is more of a high level site-architectural issue. So, you don’t need to fix is right away, because probably it doesn’t hurt your SEO. But on the other hand, if you fix this issue, your overall SEO performance could get better. Also your Crawling budget could benefit a lot, if your site or blog posts do contain many images…
As far as I understand, this is what the issue is all about: All the links overlaying the images (which are inserted by the WordPress by default) are wasting link juice and confusing the Google spiders (crawler).
Why? Because these links are dead ends, where the Google robot tries to discover what’s behind it and just gets to the image URL. This means, your crawl budget (The number of pages Google will crawl on your site on any given day) gets wasted.
How do we fix this?
Theoretically we just need to remove the image link, or to be more specific the <a href= > tag from images.
But WordPress inserts these links practically by default. And of curse there is no button to disable this behavior, so how do we do it? With a function of course 😁
To be more specifically there are 2 Functions which we could use. One is more of a quick and dirty solution with regex replace, and the second way is what I would call the proper way to do this.
Before you proceed, be sure to create a backup of your site! You should only modify a child themes function.php file, to don’t get it overwritten by theme updates.
And also please be advised, that many plugins like a ‘Image Light-box’ may not work correctly when these links are removed. Shouldn’t be a big deal, your images should be scaled to be visible without zooming anyway, but I wanted to point this out.
Maybe i should have added this info 👆 in a WordPress footnote, check out our guide on how to do that 😉
How To Remove Default Image Links in WordPress
1 Quick and Dirty: Removing via regex
/** Disable Image Links (a tag) **/
add_filter( 'the_content', 'attachment_image_link_remove_filter' );
function attachment_image_link_remove_filter( $content ) {
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" /></a>}'),
array('<img','" />'),
$content
);
return $content;
}
Code language: PHP (php)
Even knowing this will work for most of you out there, please be advised this could break some code in your pages. It just searches programmatically for the <a href …> tag on your images and replaces it just before the site is delivered to the browser of your visitor. I strongly recommend you the Second option 👇
2 The Proper way: Disabling it
Okay, so to proper disable the WordPress behavior of adding a link to every image you insert in your posts, we first need to disable this and second, need to remove it from images already present in your posts.
2.1 Disabling the Link-placement on WordPress Images
function wpzeus_unsetimagelinking_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpzeus_unsetimagelinking_setup', 10);
Code language: PHP (php)
The code snippet above will tell WordPress to set the image_default_link_type to none. After this snippet is executed, this setting will be stored in your Database. Once this snippet is executed (i.e. when you save the functions.php or visit the frontend), you can remove it from your functions.php. Or use the Plugin Code Snippets, to execute it only once.
2.2 Remove existing self linking images in WordPress
After we made sure no more Image links will be added in the future, we need to remove the existing ones. I Found this function snippet here and it was working very well on all of my sites.
function remove_self_linking_images() {
$all_ids = new WP_Query(array(
'post_type' => array('post', 'page'), // feel free to add custom post types here if necessary
'posts_per_page' => -1,
'post_status' => 'any',
'fields' => 'ids'
));
foreach($all_ids->posts as $id) {
$current_post = get_post($id);
$current_content = $current_post->post_content;
$content_replaced = $current_content;
preg_match_all('/<a.*?href=\"(.*?)\".*?>(<img.*?src=\"(.*?)\".*?>)<\/a>/', $current_content, $matches);
$wp_selfies = $matches[];
if(count($wp_selfies) > ) {
$wp_selfies_urls = $matches[1];
$wp_selfies_imgs = $matches[2];
$wp_selfies_srcs = $matches[3];
foreach($wp_selfies as $key => $selfie) {
if($wp_selfies_urls[$key] === $wp_selfies_srcs[$key]) {
$content_replaced = str_replace($selfie, $wp_selfies_imgs[$key], $content_replaced);
}
}
wp_update_post(array(
'ID' => $id,
'post_content' => $content_replaced
));
}
}
}
add_action('admin_init', 'remove_self_linking_images');
Code language: PHP (php)
Again, you only need to execute this function once. As soon as it was executed, you should no longer see any image links appearing on your WordPress Site 👇
That’s all, your image links are gone now 😎
If you would like to learn more about image handling, we have in interesting guide on how you can add query parameters to any WordPress image URL, depending on the thumbnail size. Very useful for CDNs that support parameters.
We love 💚 Feedback, so if you have a questions left or it didn’t work for you, feel free to drop us a comment 👇
“2.2 Remove existing self linking images in WordPress”, code is broken. I tried adding to my WordPress website using Code Snippet plugin!
Hi Himanshu,
the Code-Snippet is working for me. Is anyone else having issues?
Either the code from that referring link was copied incorrectly, or the code has been updates as follows and now works correctly. Again, thank you kindly for providing the solution to this issue. Best, Patrick
Sure thing, glad I could help you out!
Thanks. Is there a way to set a website up from the start so that this doesn’t happen? A setting – rather than code to fix retrospectively. Thanks.
Yes, as illustrated it is the second method described in the post