<?php
/*
Plugin Name: Broken Link Checker
Description: Crawls content and checks for broken links, then reports them.
Version: 1.0
Author: Your Name
*/

// Function to check for broken links
function check_broken_links() {
    global $wpdb;

    // Get all posts
    $posts = $wpdb->get_results("SELECT ID, post_content FROM $wpdb->posts WHERE post_status = 'publish'");

    $broken_links_report = [];

    foreach ($posts as $post) {
        $doc = new DOMDocument();
        @$doc->loadHTML($post->post_content);
        $tags = $doc->getElementsByTagName('a');

        foreach ($tags as $tag) {
            $url = $tag->getAttribute('href');

            // Skip empty URLs
            if (empty($url)) continue;

            // Check the URL
            $response = wp_remote_head($url);
            if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200) {
                $broken_links_report[] = [
                    'post_id' => $post->ID,
                    'post_title' => get_the_title($post->ID),
                    'broken_url' => $url
                ];
            }
        }
    }

    // Store the report as an option
    update_option('broken_links_report', $broken_links_report);
}

// Schedule the event on plugin activation
register_activation_hook(__FILE__, 'blc_schedule_event');
register_deactivation_hook(__FILE__, 'blc_clear_scheduled_event');

function blc_schedule_event() {
    if (!wp_next_scheduled('blc_daily_event')) {
        wp_schedule_event(time(), 'daily', 'blc_daily_event');
    }
}

function blc_clear_scheduled_event() {
    wp_clear_scheduled_hook('blc_daily_event');
}

// Hook the check_broken_links function to the scheduled event
add_action('blc_daily_event', 'check_broken_links');

// Create an admin page to display the report
add_action('admin_menu', 'blc_create_admin_page');

function blc_create_admin_page() {
    add_menu_page(
        'Broken Links Report',
        'Broken Links',
        'manage_options',
        'broken-links-report',
        'blc_display_report'
    );
}

function blc_display_report() {
    $broken_links_report = get_option('broken_links_report', []);

    echo '<div class="wrap">';
    echo '<h1>Broken Links Report</h1>';

    if (empty($broken_links_report)) {
        echo '<p>No broken links found.</p>';
    } else {
        echo '<table class="widefat">';
        echo '<thead><tr><th>Post Title</th><th>Broken URL</th></tr></thead>';
        echo '<tbody>';
        foreach ($broken_links_report as $report) {
            echo '<tr>';
            echo '<td><a href="' . get_edit_post_link($report['post_id']) . '">' . esc_html($report['post_title']) . '</a></td>';
            echo '<td><a href="' . esc_url($report['broken_url']) . '" target="_blank">' . esc_html($report['broken_url']) . '</a></td>';
            echo '</tr>';
        }
        echo '</tbody>';
        echo '</table>';
    }

    echo '</div>';
}
?>
 

PHP Online Compiler

Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP and start coding.

Taking inputs (stdin)

OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.

<?php
	fscanf(STDIN, "%s\n", $name);           
    echo "Hello ".$name.".\n";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.

$variable_name = value;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
    //code if above condition is true  
} elseif(condition-expression2){  
    //code if above condition is true  
}  
elseif(condition-expression3) {  
    //code if above condition is true  
}  
...  
else {  
    //code if all the conditions are false  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(conditional-expression) {    
case value1:    
 // code if the above value is matched    
 break;  // optional  
case value2:    
 // code if the above value is matched    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)