The addition of a lottery Application to our local club and the restrictions working with lotto providers got me thinking. How can I generate lotto numbers and more importantly make a plugin to allow members to play it online while removing the 6% to 10% the provider charges for the service.
I went to ChatGPT and asked how to generate lotto number as a quick pick but also as a user input.
This is a work in progress and I will revisit this later in the year, but I think it may have some legs. I need to clean the code up add a user form and a payment processing.
Of course there are many paid plugins which already offer a solution , once I have reviewed this further we may revisit this…
Lottery Application PHP Plugin Code
<?php
/**
Plugin Name: Lotto Number Generator
Description: Generates and stores lotto numbers.
Version: 1.0
Author: Anything access
Author URI: https://AnythingAccess.com
*/
// Prevent direct access.
if (!defined('ABSPATH')) {
exit;
}
// Create database table on activation.
function lotto_plugin_activate() {
global $wpdb;
$table_name = $wpdb->prefix . 'lotto_numbers';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
numbers VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'lotto_plugin_activate');
// Shortcode to generate lotto numbers.
function generate_lotto_shortcode($atts) {
global $wpdb;
// Default attributes.
$atts = shortcode_atts(
['count' => 6, 'max' => 49],
$atts,
'generate_lotto'
);
$count = intval($atts['count']);
$max = intval($atts['max']);
// Generate random numbers.
$numbers = [];
while (count($numbers) < $count) {
$num = rand(1, $max);
if (!in_array($num, $numbers)) {
$numbers[] = $num;
}
}
sort($numbers);
$numbers_str = implode(', ', $numbers);
// Store in database.
$table_name = $wpdb->prefix . 'lotto_numbers';
$wpdb->insert($table_name, ['numbers' => $numbers_str]);
return "<p>Lotto Numbers: $numbers_str</p>";
}
add_shortcode('generate_lotto', 'generate_lotto_shortcode');
// Admin menu for viewing results.
function lotto_admin_menu() {
add_menu_page(
'Lotto Results',
'Lotto Results',
'manage_options',
'lotto-results',
'lotto_admin_page',
'dashicons-tickets-alt',
20
);
}
add_action('admin_menu', 'lotto_admin_menu');
// Admin page content.
function lotto_admin_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'lotto_numbers';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC");
echo '<div class="wrap">';
echo '<h1>Lotto Results</h1>';
echo '<table class="widefat fixed" cellspacing="0">';
echo '<thead><tr><th>ID</th><th>Numbers</th><th>Date</th></tr></thead>';
echo '<tbody>';
foreach ($results as $result) {
echo '<tr>';
echo '<td>' . $result->id . '</td>';
echo '<td>' . $result->numbers . '</td>';
echo '<td>' . $result->created_at . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
If you already have an online lottery application , you might be interested in our free offline lottery application
