Dorm management

WPTurbo » Snippets » Dorm management
0

Created with:

Visibility: 

public

Creator: Steve

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?<?php
/*
Plugin Name: Dorm Management
Description: Manage dorms and student assignments for WPSchoolPress
Version: 1.0
Author: Ofx Steve
*/

// Create dorms table on plugin activation
register_activation_hook(__FILE__, 'dm_create_tables');
function dm_create_tables() {
    global $wpdb;
    $table_name = $wpdb->prefix . "dorms";
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(100) NOT NULL,
        capacity int(5) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

// Add admin menu
add_action('admin_menu', 'dm_admin_menu');
function dm_admin_menu() {
    add_menu_page('Dorm Management', 'Dorms', 'manage_options', 'dorm-management', 'dm_dashboard', 'dashicons-building', 6);
}

// Dashboard page
function dm_dashboard() {
    global $wpdb;
    $table_name = $wpdb->prefix . "dorms";

    // Handle form submission
    if(isset($_POST['dm_add_dorm'])) {
        $name = sanitize_text_field($_POST['name']);
        $capacity = intval($_POST['capacity']);
        $wpdb->insert($table_name, array(
            'name' => $name,
            'capacity' => $capacity
        ));
        echo "<div class='updated'><p>Dorm added successfully!</p></div>";
    }

    // Display form
    echo '<h1>Dorm Management</h1>';
    echo '<form method="post">';
    echo 'Dorm Name: <input type="text" name="name" required>';
    echo ' Capacity: <input type="number" name="capacity" required>';
    echo ' <input type="submit" name="dm_add_dorm" value="Add Dorm">';
    echo '</form>';

    // Display dorms list
    $dorms = $wpdb->get_results("SELECT * FROM $table_name");
    if($dorms) {
        echo '<h2>Existing Dorms</h2>';
        echo '<table border="1" cellpadding="5"><tr><th>ID</th><th>Name</th><th>Capacity</th></tr>';
        foreach($dorms as $dorm) {
            echo "<tr><td>{$dorm->id}</td><td>{$dorm->name}</td><td>{$dorm->capacity}</td></tr>";
        }
        echo '</table>';
    } else {
        echo '<p>No dorms added yet.</p>';
    }
}

// Optional: Assign students to dorms
// You can expand this section to pull students from WPSchoolPress
				

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