WordPress Radio Player Stats Plugin by ChatGPT

So, the stats from Radio Player are somewhat broken. I just wanted to see what is the most popular program on our community radio station website. Here’s what ChatGPT knocked up:

In public_html/wp-content/plugins create a folder called radio-player-analytics

Create a file therein called radio-player-analytics.php.

<?php
/**
 * Plugin Name: Radio Player Analytics
 * Description: Admin analytics for Radio Player listening time.
 * Version: 1.1.1
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/* Add menu */
add_action( 'admin_menu', 'rpa_add_menu' );
function rpa_add_menu() {
    add_menu_page(
        'Radio Player Analytics',
        'Radio Analytics',
        'manage_options',
        'radio-player-analytics',
        'rpa_render_admin_page',
        'dashicons-chart-bar',
        26
    );
}

/* Render page */
function rpa_render_admin_page() {
    global $wpdb;

$stats   = $wpdb->prefix . 'radio_player_statistics';
$players = $wpdb->prefix . 'radio_player_players';

    $sql = "
        SELECT
            s.station,
            IFNULL(p.title, 'Live Stream') AS station_name,
            SUM(s.duration) AS total_seconds,
            AVG(s.duration) AS avg_seconds,
            COUNT(*) AS sessions
        FROM {$stats} s
        LEFT JOIN {$players} p ON p.id = s.station
        WHERE s.duration >= 20
        GROUP BY s.station, p.title
        ORDER BY total_seconds DESC
        LIMIT 15
    ";

    $results = $wpdb->get_results( $sql );

    echo '<div class="wrap">';
    echo '<h1>📻 Radio Player Analytics</h1>';
    echo '<p><em>Listening sessions under 20 seconds are excluded.</em></p>';

    if ( $wpdb->last_error ) {
        echo '<div class="notice notice-error"><p>';
        echo esc_html( $wpdb->last_error );
        echo '</p></div>';
        echo '</div>';
        return;
    }

    echo '<table class="widefat striped">';
    echo '<thead><tr>
            <th>Rank</th>
            <th>Station</th>
            <th>Total Listening (hrs)</th>
            <th>Avg Session</th>
            <th>Sessions</th>
          </tr></thead><tbody>';

    if ( $results ) {
        $rank = 1;
        foreach ( $results as $row ) {
            echo '<tr>';
            echo '<td>' . (int) $rank++ . '</td>';
            echo '<td><strong>' . esc_html( $row->station_name ) . '</strong></td>';
            echo '<td>' . round( $row->total_seconds / 3600, 2 ) . '</td>';
            echo '<td>' . gmdate( 'i:s', (int) $row->avg_seconds ) . '</td>';
            echo '<td>' . (int) $row->sessions . '</td>';
            echo '</tr>';
        }
    } else {
        echo '<tr><td colspan="5">No data available.</td></tr>';
    }

    echo '</tbody></table>';
    echo '</div>';
}

Then, enable the plugin.

What I don't know is what date range it selects.

Leave a Reply

Your email address will not be published. Required fields are marked *