What is the WordPress plugin to check any broken links? The plugin would need to regularly scan your website’s pages and posts for links, and then check each link to see if it’s valid. Here is an example of how you can create a plugin to check for broken links:

  1. Create a new folder in the wp-content/plugins directory and name it broken-link-checker.
  2. Inside that folder, create a new PHP file named broken-link-checker.php. This will be the main plugin file.
  3. In the broken-link-checker.php file, add the plugin information at the top, such as the plugin name, description, and version.
<?php
/*
Plugin Name: Broken Link Checker
Plugin URI: https://example.com/broken-link-checker
Description: This plugin checks for broken links on your website and logs them in a custom database table.
Version: 1.0
Author: Your Name
Author URI: https://example.com
*/
  1. Next, create a custom database table to store the broken link logs. You can use the dbDelta() function to create the table.
function create_broken_link_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'broken_links';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        url varchar(255) NOT NULL,
        post_id bigint(20) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
  1. Now, you can add a function that will be called to check all the links on your website. This function will log the broken links in the custom table created before.
function check_broken_links() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'broken_links';

    // Get all the pages and posts on the website
    $posts = get_posts( array(
        'post_type'   => array( 'page', 'post' ),
        'post_status' => 'publish',
        'numberposts' => -1,
    ) );

    // Iterate through each post and check the links
    foreach ( $posts as $post ) {
        $dom = new DOMDocument;
        @$dom->loadHTML( $post->post_content );
        $links = $dom->getElementsByTagName( 'a' );

        // Iterate through each link
        foreach ( $links as $link ) {
            $url = $link->getAttribute( 'href' );

            // Check if the link is valid
            $headers = get_headers( $url, 1 );
            if ( strpos( $headers[0], '404' ) !== false ) {
                $wpdb
  1. You can check if the link is valid by checking the HTTP status code of the link. A status code of 200 indicates that the link is valid, while a status code of 404 or similar indicates that the link is broken.
$headers = get_headers($url);
$status = substr($headers[0], 9, 3);
if ($status == '404') {
    $wpdb->insert(
    $table_name,
    array(
        'time' => current_time('mysql'),
        'url' => $url,
        'post_id' => $post->ID,
    )
);
  1. Finally, you need to activate the plugin and schedule the function to be called on regular intervals. You can do this by adding the following code at the bottom of the plugin file.
register_activation_hook( __FILE__, 'create_broken_link_table' );
if ( ! wp_next_scheduled( 'check_broken_links' ) ) {
    wp_schedule_event( time(), 'daily', 'check_broken_links' );
}
add_action( 'check_broken_links', 'check_broken_links' );

This is just an example, you can customize the plugin as per your requirement. Please note that this code is for demonstration purposes only, and you should thoroughly test and debug your plugin before using it on a live website. If you are not familiar with these technologies, I would recommend hiring a WordPress developer like Defyn Digital based in Penrith with experience in plugin development to assist you. Please let me know if you have any other questions.

READY TO RE-DEFYN YOUR BUSINESS?