Below is a **complete WordPress plugin** that automatically **creates a sample table `wp_cdat_students` on activation**.
<?php
/**
* Plugin Name: CDAT Students CRUD
* Description: Simple CRUD interface for cdat_students table.
* Version: 1.0
* Author: Civil Dept
*/
// -----------------------------------------------------------------------------
// 1. CREATE TABLE ON ACTIVATION
// -----------------------------------------------------------------------------
register_activation_hook(__FILE__, 'cdat_create_students_table');
function cdat_create_students_table() {
global $wpdb;
$table = $wpdb->prefix . "cdat_students";
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(200) NOT NULL,
regno VARCHAR(50) NOT NULL,
semester INT(11) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id)
) $charset;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// -----------------------------------------------------------------------------
// 2. ADMIN MENU
// -----------------------------------------------------------------------------
add_action('admin_menu', 'cdat_students_menu');
function cdat_students_menu() {
add_menu_page(
"Students", "Students", "manage_options",
"cdat-students", "cdat_students_page",
"dashicons-welcome-learn-more", 34
);
}
// -----------------------------------------------------------------------------
// 3. MAIN PAGE LOGIC (CRUD)
// -----------------------------------------------------------------------------
function cdat_students_page() {
global $wpdb;
$table = $wpdb->prefix . "cdat_students";
echo "<h1>CDAT Students</h1>";
// ------------------------
// DELETE ACTION
// ------------------------
if (isset($_GET['action']) && $_GET['action'] == "delete" && isset($_GET['id'])) {
$id = intval($_GET['id']);
if (!wp_verify_nonce($_GET['_wpnonce'], "cdat_delete_$id")) {
die("Security check failed");
}
$wpdb->delete($table, ['id' => $id]);
echo "<div class='updated'><p>Deleted Successfully</p></div>";
}
// ------------------------
// SAVE NEW / EDIT
// ------------------------
if (isset($_POST['cdat_save_student'])) {
$data = [
"name" => sanitize_text_field($_POST['name']),
"regno" => sanitize_text_field($_POST['regno']),
"semester" => intval($_POST['semester']),
];
if ($_POST['id'] == 0) {
$wpdb->insert($table, $data);
echo "<div class='updated'><p>Student Added.</p></div>";
} else {
$wpdb->update($table, $data, ['id' => intval($_POST['id'])]);
echo "<div class='updated'><p>Student Updated.</p></div>";
}
}
// ------------------------
// EDIT FORM
// ------------------------
$edit = null;
if (isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['id'])) {
$id = intval($_GET['id']);
$edit = $wpdb->get_row("SELECT * FROM $table WHERE id=$id");
}
?>
<h2><?php echo $edit ? "Edit Student" : "Add Student"; ?></h2>
<form method="post">
<input type="hidden" name="id" value="<?php echo $edit ? $edit->id : 0; ?>">
<table class="form-table">
<tr>
<th>Name:</th>
<td><input type="text" name="name" required value="<?php echo $edit ? esc_attr($edit->name) : ""; ?>"></td>
</tr>
<tr>
<th>Reg No:</th>
<td><input type="text" name="regno" required value="<?php echo $edit ? esc_attr($edit->regno) : ""; ?>"></td>
</tr>
<tr>
<th>Semester:</th>
<td><input type="number" name="semester" min="1" max="8" required value="<?php echo $edit ? esc_attr($edit->semester) : ""; ?>"></td>
</tr>
</table>
<button class="button button-primary" type="submit" name="cdat_save_student">
<?php echo $edit ? "Update" : "Add"; ?>
</button>
</form>
<hr>
<h2>All Students</h2>
<?php
$rows = $wpdb->get_results("SELECT * FROM $table ORDER BY id DESC");
if (!$rows) {
echo "<p>No students found.</p>";
return;
}
?>
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Reg No</th>
<th>Semester</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $r): ?>
<tr>
<td><?php echo $r->id; ?></td>
<td><?php echo esc_html($r->name); ?></td>
<td><?php echo esc_html($r->roll); ?></td>
<td><?php echo $r->semester; ?></td>
<td>
<a href="?page=cdat-students&action=edit&id=<?php echo $r->id; ?>" title="Edit">
<span class="dashicons dashicons-edit"></span>
</a>
<a href="<?php echo wp_nonce_url("?page=cdat-students&action=delete&id=$r->id", "cdat_delete_$r->id"); ?>"
onclick="return confirm('Delete this record?')" title="Delete">
<span class="dashicons dashicons-trash"></span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
?>
### 1. Install Plugin
Put the file `cdat-students.php` inside it.
Activate the plugin.
# 2. Table Automatically Created
And inserts sample records.
# 3. Display Student Table
No comments:
Post a Comment