-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud-plugin.php
123 lines (110 loc) · 3.77 KB
/
crud-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
*
* Plugin Name: CRUD Plugin
* Description: A Simple CRUD Plugin for WodPress
* Version: 1.0
* Author: Dinesh Suthar
*/
if(!defined('ABSPATH')) exit;
register_activation_hook(__FILE__, 'crud_plugin_create_table');
function crud_plugin_create_table()
{
global $wpdb;
$table_name = $wpdb->prefix . 'crud_items';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
description text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_deactivation_hook(__FILE__, 'crud_plugin_drop_table');
function crud_plugin_drop_table(){
global $wpdb;
$table_name = $wpdb->prefix . 'crud_items';
$sql = "DROP TABLE IF EXISTS $table_name;";
$wpdb->query($sql);
}
add_action('admin_menu', 'crud_plugin_admin_menu');
function crud_plugin_admin_menu(){
add_menu_page(
'CRUD Items',
'CRUD Items',
'manage_options',
'crud-items',
'crud_plugin_admin_page',
'dashicons-list-view',
6
);
}
function crud_plugin_admin_page(){
global $wpdb;
$table_name = $wpdb->prefix . 'crud_items';
if(isset($_POST['submit'])){
$title = sanitize_text_field($_POST['title']);
$description = sanitize_textarea_field($_POST['description']);
if(!empty($title) && !empty($description)){
$wpdb->insert(
$table_name,
array(
'title' => $title,
'description' => $description,
)
);
echo '<div class="updated"><p> Item added successfully!</p></div>';
}
}
?>
<div class="wrap">
<h1>Crud Items</h1>
<form method="POST">
<table class="form-table">
<tr>
<th scope="row"><label for="title">Title</label></th>
<td><input type="text" name="title" id="title" class="regular-text"></td>
</tr>
<tr>
<th scope="row"><label for="description">Description</label></th>
<td><textarea name="description" id="description" rows="5" class="large-text"></textarea></td>
</tr>
</table>
<p><input type="submit" name="submit" id="submit" class="button button-primary" value="Add Item"></p>
</form>
<hr>
<h2>All Items</h2>
<table class="widefat fixed" cellspacing="0">
<thead>
<tr>
<th id="columnname" class="manage-column column-columnname" scope="col">Title</th>
<th id="columnname" class="manage-column column-columnname" scope="col">Description</th>
<th id="columnname" class="manage-column column-columnname" scope="col">Actions</th>
</tr>
<thead>
<tbody>
<?php
$results = $wpdb->get_results("SELECT * FROM $table_name");
foreach($results as $row){
echo "<tr>";
echo "<td>{$row->title}</td>";
echo "<td>{$row->description}</td>";
echo "<td><a href='?page=crud-items&delete={$row->id}' class='button button-danger'>Delete</a></td>";
echo "<tr>";
}
?>
</tbody>
</theading>
</table>
</div>
<?php
if(isset($_GET['delete'])){
$id = intval($_GET['delete']);
$wpdb->delete($table_name, ['id' => $id]);
echo '<div class="updated"><p>Item deleted successfully!</p></div>';
echo "<script>location.replace('?page=crud-items');</script>";
}
}
?>