WordPress : How to Find 404 URL’S in the site

How to Find 404 URLs in a WordPress Site via CLI

Finding and fixing 404 errors is crucial for maintaining a healthy WordPress site. The code provided defines a custom WP-CLI command to find 404 URLs on your site. Here’s how to use it.

Prerequisites

  • SSH access to your server
  • WP-CLI installed on your server

Step-by-Step Guide

1. Access Your Server via SSH

ssh username@your_server_ip

2. Place the PHP Script

Save the PHP script to your WordPress plugin directory:

<?php
if ( ! class_exists( 'WP_CLI' ) ) {
    return;
}

class ACJWP_Inbody_404_Urls_Cli {
    public function __invoke( $args, $args_assoc ) {
        $this->perform_bulk_operations( true );
        $args = array();
        $between = array( '2016-06-01', date( 'Y-m-d' ) );
        if ( ! empty( $args_assoc['between'] ) ) {
            $is_date = preg_match( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]),[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $args_assoc['between'] );
            if ( ! $is_date ) {
                WP_CLI::log( 'Please provide --between param in YYYY-MM-DD,YYYY-MM-DD format' );
                return;
            }
            $between = explode( ',', $args_assoc['between'] );
        }
        if ( ! empty( $args_assoc['post_types'] ) ) {
            $args['post_types'] = explode( ',', $args_assoc['post_types'] );
        }
        if ( ! empty( $args_assoc['post_status'] ) ) {
            $args['post_status'] = explode( ',', $args_assoc['post_status'] );
        }
        if ( ! empty( $args_assoc['limit'] ) ) {
            $args['limit'] = $args_assoc['limit'];
        }
        $not_found_url_posts = [];
        $not_found_url = new ACJWP_Inbody_404_Urls_Finder();
        $start_date = $between[0];
        while ( $start_date <= $between[1] ) {
            WP_CLI::log( sprintf( 'Running for date: %s', $start_date ) );
            $next_date = date( 'Y-m-d', strtotime( $start_date . '+1 day' ) );
            $args['between'] = array( $start_date, $next_date );
            $urls = $not_found_url->get_all_404s( $args );
            WP_CLI::log( sprintf( 'Total processed data for date %s is %d', $start_date, count( $urls ) ) );
            if ( empty( $urls ) ) {
                WP_CLI::log( 'No posts found.' );
                $start_date = $next_date;
                continue;
            }
            $not_found_url_posts = array_replace( $not_found_url_posts, $urls );
            $start_date = $next_date;
            sleep( 1 );
            $this->start_inmemory_cleanup();
        }
        WP_CLI::log( sprintf( 'Gross Total Posts # %d', count( $not_found_url_posts ) ) );
        WP_CLI::Success( wp_json_encode( $not_found_url_posts ) );
        WP_CLI::log( 'Run Ends' );
        $this->perform_bulk_operations( false );
    }

    public function perform_bulk_operations( bool $defer ) {
        wp_defer_term_counting( $defer );
    }

    public function start_inmemory_cleanup() {
        global $wpdb, $wp_object_cache;
        $wpdb->queries = array();
        if ( ! is_object( $wp_object_cache ) ) {
            return;
        }
        $wp_object_cache->group_ops = array();
        $wp_object_cache->memcache_debug = array();
        $wp_object_cache->cache = array();
        if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
            $wp_object_cache->__remoteset(); // important
        }
    }
}

WP_CLI::add_command( 'inbody404', 'ACJWP_Inbody_404_Urls_Cli' );

3. Run the WP-CLI Command

wp inbody404

After adding the script, run the custom command from the terminal:

You can specify parameters like --between, --post_types, --post_status, and --limit to customize the search.

Conclusion

By using this custom WP-CLI command, you can efficiently find 404 URLs in your WordPress site. This approach ensures better site maintenance and improved user experience.

For detailed steps and code, refer to the original WordPress Plugin that I have created to find all the 404 URLs on the site.

Agile Agile Methodology AsyncStorage Dependency Inversion Principle Gutenberg Hooks Integration Testing JSON justify content center Justify text Migration Mindset Node Npm ObjectOriented design Performance Testing PHP React Js React Native React Native Buttons React Native Elements React Native Error React Native IOS React Navigation redux redux-persist redux-persist-react-native-async-storage Regression Testing Security Testing SOLID principles Unit Testing useDispatch User Acceptance Testing UAT WordPress WordPress Simplification WordPressVip WP Block Patters WP Gutenberg WP Gutenberg Patterns