Thursday, September 18, 2025

#4 Attendance

Perfect ✅ Let’s build a single-file WordPress plugin for your requirement.

This plugin will:

  1. Create a DB table for attendance.

  2. Provide a page with Registration Entry Form (Name, Institution, Department, City, Date, Workshop, Photo).

  3. Upload photos (only JPG < 100KB).

  4. Allow viewing entries and print PDF Certificates using Dompdf (bundled in WP plugin).


πŸ”Ή Complete Single-File Plugin Code

Save this as workshop-attendance.php inside your wp-content/plugins/ folder. Then activate in WP Admin → Plugins.

<?php
/**
 * Plugin Name: Workshop Attendance & Certificates
 * Description: Manage workshop attendance with registration form and PDF certificate printing.
 * Version: 1.0
 * Author: OpenAI
 */

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

class Workshop_Attendance {
    private $table;

    public function __construct() {
        global $wpdb;
        $this->table = $wpdb->prefix . 'workshop_attendance';

        register_activation_hook(__FILE__, [$this, 'install']);
        add_shortcode('workshop_form', [$this, 'form_page']);
        add_shortcode('workshop_list', [$this, 'list_page']);
        add_action('init', [$this, 'handle_form']);
    }

    /** Create DB table */
    public function install() {
        global $wpdb;
        $charset = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE {$this->table} (
            id BIGINT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(200),
            institution VARCHAR(200),
            department VARCHAR(200),
            city VARCHAR(200),
            wdate DATE,
            workshop VARCHAR(200),
            photo VARCHAR(255),
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        ) $charset;";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

    /** Handle form submission */
    public function handle_form() {
        if (isset($_POST['wa_submit'])) {
            global $wpdb;

            // Photo validation
            if (!empty($_FILES['photo']['name'])) {
                $file = $_FILES['photo'];
                if ($file['type'] !== 'image/jpeg') {
                    wp_die('Only JPG images allowed');
                }
                if ($file['size'] > 100 * 1024) {
                    wp_die('File size must be less than 100KB');
                }
                $upload = wp_upload_bits($file['name'], null, file_get_contents($file['tmp_name']));
                if ($upload['error']) {
                    wp_die('Upload error: ' . $upload['error']);
                }
                $photo_url = $upload['url'];
            } else {
                $photo_url = '';
            }

            $wpdb->insert($this->table, [
                'name'        => sanitize_text_field($_POST['name']),
                'institution' => sanitize_text_field($_POST['institution']),
                'department'  => sanitize_text_field($_POST['department']),
                'city'        => sanitize_text_field($_POST['city']),
                'wdate'       => sanitize_text_field($_POST['wdate']),
                'workshop'    => sanitize_text_field($_POST['workshop']),
                'photo'       => $photo_url,
            ]);

            wp_redirect(add_query_arg('success', '1', get_permalink()));
            exit;
        }
    }

    /** Registration Form */
    public function form_page() {
        ob_start();
        if (isset($_GET['success'])) {
            echo '<p style="color:green;">✅ Registration saved successfully!</p>';
        }
        ?>
        <h2>Workshop Attendance Registration</h2>
        <form method="post" enctype="multipart/form-data">
            <p><input type="text" name="name" placeholder="Full Name" required></p>
            <p><input type="text" name="institution" placeholder="Institution" required></p>
            <p><input type="text" name="department" placeholder="Department" required></p>
            <p><input type="text" name="city" placeholder="City" required></p>
            <p><input type="date" name="wdate" required></p>
            <p><input type="text" name="workshop" placeholder="Workshop Title" required></p>
            <p>Upload Photo (JPG, <100KB): <input type="file" name="photo" accept="image/jpeg" required></p>
            <p><button type="submit" name="wa_submit">Submit</button></p>
        </form>
        <?php
        return ob_get_clean();
    }

    /** List & Certificates */
    public function list_page() {
        global $wpdb;
        $rows = $wpdb->get_results("SELECT * FROM {$this->table} ORDER BY created_at DESC");

        ob_start();
        ?>
        <h2>Workshop Attendance List</h2>
        <table border="1" cellpadding="6" cellspacing="0">
            <tr>
                <th>ID</th><th>Name</th><th>Institution</th><th>Dept</th><th>City</th><th>Date</th><th>Workshop</th><th>Photo</th><th>Certificate</th>
            </tr>
            <?php foreach ($rows as $r): ?>
            <tr>
                <td><?= $r->id ?></td>
                <td><?= esc_html($r->name) ?></td>
                <td><?= esc_html($r->institution) ?></td>
                <td><?= esc_html($r->department) ?></td>
                <td><?= esc_html($r->city) ?></td>
                <td><?= esc_html($r->wdate) ?></td>
                <td><?= esc_html($r->workshop) ?></td>
                <td><?php if ($r->photo): ?><img src="<?= esc_url($r->photo) ?>" width="60"><?php endif; ?></td>
                <td><a href="<?= add_query_arg(['wa_cert' => $r->id]) ?>" target="_blank">πŸ–¨ Print PDF</a></td>
            </tr>
            <?php endforeach; ?>
        </table>
        <?php

        // Certificate generation
        if (isset($_GET['wa_cert'])) {
            $id = intval($_GET['wa_cert']);
            $student = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table} WHERE id=%d", $id));
            if ($student) {
                require_once __DIR__ . '/dompdf/autoload.inc.php';
                $dompdf = new Dompdf\Dompdf();
                $html = "
                <h2 style='text-align:center;'>Certificate of Participation</h2>
                <p style='text-align:center;'>This is to certify that <strong>{$student->name}</strong><br>
                from <strong>{$student->institution}</strong>, Department of {$student->department}, {$student->city}<br>
                has successfully attended the workshop <strong>{$student->workshop}</strong><br>
                held on {$student->wdate}.</p>";
                $dompdf->loadHtml($html);
                $dompdf->render();
                $dompdf->stream("certificate-{$student->id}.pdf");
                exit;
            }
        }

        return ob_get_clean();
    }
}

new Workshop_Attendance();

πŸ”Ή How to Use

  1. Upload workshop-attendance.php into wp-content/plugins/.

  2. Also download Dompdf, extract, and place it inside wp-content/plugins/workshop-attendance/dompdf/.

  3. Activate plugin in WordPress.

  4. Create 2 pages:

    • Registration Form Page: put shortcode [workshop_form]

    • Attendance List Page (Admin): put shortcode [workshop_list]


✅ Now:

  • Students → fill the Attendance Form.

  • Admin → view Attendance List + click Print PDF for certificates.


Do you want me to also make the Certificates printable in Tamil + English (like Hall Ticket earlier)?

No comments:

Post a Comment

#8 Make WP Theme

 Perfect πŸ‘ Let’s upgrade the minimal theme with all the “nice-to-have” Astra-like enhancements so it feels more professional, flexible, an...