I’d think every web developer would know what a broken link checker is , but for those who don’t , it’s a tool that checks for broken links on a website. Broken links, also known as dead links or 404 errors, are links that lead to a webpage that no longer exists or cannot be accessed.

There are several reasons why you might want to use a broken link checker:

  1. User experience: Broken links can be frustrating for users and can lead to a poor user experience. If a user clicks on a broken link, they will be taken to a 404 error page, which can be confusing and cause them to leave the website.
  2. SEO: Search engines use links to discover and index webpages. If a search engine encounters a broken link, it will be unable to index the webpage and this can affect the ranking of your website.
  3. Maintenance: A broken link checker can help you identify and fix broken links on your website, which can help you maintain a healthy and up-to-date website.

So if you ever think to build a simple one in Wordpress here is how you could get started :

<?php

/*
Plugin Name: Wordpress Broken Link Checker
Description: Checks for broken links on all pages of your website and displays a report.
*/

// Hook into the 'wp_footer' action
add_action('wp_footer', 'check_broken_links');

function check_broken_links() {
  // Get all the pages on the website
  $pages = get_pages();

  // Initialize an array to store broken links
  $broken_links = array();

  // Check each page for broken links
  foreach ($pages as $page) {
    $links = get_page_links($page->ID);
    foreach ($links as $link) {
      $status = get_link_status($link);
      if ($status != 200) {
        // Link is broken, add it to the list of broken links
        $broken_links[] = $link;
      }
    }
  }

  // Display a report of broken links
  if (count($broken_links) > 0) {
    echo '<h2>Broken Link Report</h2>';
    echo '<ul>';
    foreach ($broken_links as $link) {
      echo "<li>$link</li>";
    }
    echo '</ul>';
  }
}

function get_page_links($page_id) {
  // Get the HTML content of the page
  $html = get_post_field('post_content', $page_id);

  // Use regular expressions to extract all the links from the HTML
  preg_match_all('/<a href="([^"]+)"/', $html, $matches);
  return $matches[1];
}

function get_link_status($url) {
  // Send a HEAD request to the link to check its status
  $headers = @get_headers($url);
  if ($headers) {
    // Return the HTTP status code
    return substr($headers[0], 9, 3);
  }
  else {
    // Return a non-200 status code if the link is broken
    return 0;
  }
}

This plugin gets a list of all the pages on the Wordpress website using the get_pages function and then checks each page for broken links. It does this by extracting all the links from the page’s HTML content and checking the status of each link using the get_link_status function. If a link has a status code other than 200, it is added to the list of broken links. Finally, the plugin displays a report of broken links at the bottom of the page.

Have a great day 🙂

Categorized in: