Create a plugin to scan local.ch, and extract site data including: email, website, metaname generator, wordpress version

Home » Snippets » Create a plugin to scan local.ch, and extract site data including: email, website, metaname generator, wordpress version
0

Created with:

Visibility: 

public

Creator: info@webmasterseo.ch

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: Local.ch Site Data Scanner
 * Plugin URI: https://example.com/
 * Description: Scans local.ch and extracts site data including email, website, meta name generator, and WordPress version.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://example.com/
 * License: GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: wpturbo
 */

// Hook into the WordPress admin init action
add_action( 'admin_init', 'wpturbo_local_ch_scanner' );

/**
 * Local.ch Site Data Scanner
 */
function wpturbo_local_ch_scanner() {
    // Check if the user has the necessary capability to perform the scan
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Perform the scan
    $site_data = wpturbo_scan_local_ch();
    
    // Display the scanned site data on the WordPress admin page
    add_action( 'admin_notices', function() use ( $site_data ) {
        echo '<div class="notice notice-success"><p>';
        echo 'Email: ' . $site_data['email'] . '<br>';
        echo 'Website: ' . $site_data['website'] . '<br>';
        echo 'Meta Name Generator: ' . $site_data['meta_name_generator'] . '<br>';
        echo 'WordPress Version: ' . $site_data['wordpress_version'];
        echo '</p></div>';
    } );
}

/**
 * Scan local.ch and extract site data
 *
 * @return array Site data including email, website, meta name generator, and WordPress version
 */
function wpturbo_scan_local_ch() {
    // Make a request to local.ch
    $response = wp_remote_get( 'https://www.local.ch/' );

    // Check if the request was successful
    if ( is_wp_error( $response ) ) {
        return __( 'Failed to retrieve site data', 'wpturbo' );
    }
    
    // Get the HTML content of the response
    $html = wp_remote_retrieve_body( $response );
    
    // Extract the email, website, meta name generator, and WordPress version
    $email = wpturbo_extract_email( $html );
    $website = wpturbo_extract_website( $html );
    $meta_name_generator = wpturbo_extract_meta_name_generator( $html );
    $wordpress_version = wpturbo_extract_wordpress_version();
    
    // Return the extracted site data
    return array(
        'email' => $email,
        'website' => $website,
        'meta_name_generator' => $meta_name_generator,
        'wordpress_version' => $wordpress_version,
    );
}

/**
 * Extract the email from the HTML content
 *
 * @param string $html The HTML content
 * @return string The extracted email
 */
function wpturbo_extract_email( $html ) {
    // Use a regular expression to extract the email
    preg_match( '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}/', $html, $matches );
    
    // Return the extracted email if found, otherwise return an empty string
    return isset( $matches[0] ) ? $matches[0] : '';
}

/**
 * Extract the website URL from the HTML content
 *
 * @param string $html The HTML content
 * @return string The extracted website URL
 */
function wpturbo_extract_website( $html ) {
    // Use a regular expression to extract the website URL
    preg_match( '/<a href="(https?://[^"]+)"/', $html, $matches );
    
    // Return the extracted website URL if found, otherwise return an empty string
    return isset( $matches[1] ) ? $matches[1] : '';
}

/**
 * Extract the meta name generator from the HTML content
 *
 * @param string $html The HTML content
 * @return string The extracted meta name generator
 */
function wpturbo_extract_meta_name_generator( $html ) {
    // Use a regular expression to extract the meta name generator
    preg_match( '/<meta name="generator" content="([^"]+)"/', $html, $matches );
    
    // Return the extracted meta name generator if found, otherwise return an empty string
    return isset( $matches[1] ) ? $matches[1] : '';
}

/**
 * Get the WordPress version
 *
 * @return string The WordPress version
 */
function wpturbo_extract_wordpress_version() {
    global $wp_version;
    return $wp_version;
}
				

Explanation:

This plugin creates a WordPress admin page that allows users with the 'manage_options' capability to scan local.ch and extract site data, including email, website, meta name generator, and WordPress version. The plugin uses the wp_remote_get() function to make a request to local.ch and retrieves the HTML content of the response. It then uses regular expressions to extract the desired data from the HTML.

The wpturbo_local_ch_scanner() function is hooked into the 'admin_init' action and performs the scan when the WordPress admin page is initialized. It checks if the user has the necessary capability to perform the scan and displays the scanned site data using the admin_notices action.

The wpturbo_scan_local_ch() function is responsible for making the request to local.ch, extracting the site data, and returning it as an array.

The wpturbo_extract_email(), wpturbo_extract_website(), and wpturbo_extract_meta_name_generator() functions use regular expressions to extract the email, website, and meta name generator from the HTML content, respectively.

The wpturbo_extract_wordpress_version() function retrieves the global $wp_version variable to get the WordPress version.

Please note that this code snippet is just an example and may need further customization and error handling to fit your specific use case.

Register an account to save your snippets or go Pro to get more features.