WordPress is a CMS. CMS stands for Content Management System. CMSs are helps to creating a website in easy way. Many pleople don’t know about Coding and programming languages, so WordPress help to develop their website in easy manner with less coding.
Plugins: Plugins are helps to CMS to enhance their capabilites and functionalities.
Here are some steps for creating a CRUD plugin in wordpress.
First you create a table on you existing wordpress site,
Copy below code on your phpmyadmin SQL editor.
1 2 3 4 5 6 |
CREATE TABLE `users` ( `id` varchar(3) NOT NULL, `name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step1: On the first we create file named “init.php”. This is a core file of the plugin where we make menu for admin sidebar and call all functions and pages. Here are some code below,
//This comment is a very main part of the plugin development which shows like this,
/*
Plugin Name: User List
Description: A simple user list for CRUD
Version: 1.0
Author: codingtasks.net
Author URI: http://codingtasks.net
*/
init.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 |
<?php /* Plugin Name: User List Description: A simple user list for CRUD Version: 1.0 Author: codingtasks.net Author URI: http://codingtasks.net */ <span style="color: #008000"><strong>// Add action to the Admin Menu</strong></span> add_action('admin_menu','phpcodertech_modifymenu'); <strong><span style="color: #008000">// On time of activation of the plugin, add the "Users" option</span></strong> function phpcodertech_modifymenu() { <span style="color: #008000"><strong>// The main item for the menu</strong></span> add_menu_page('users', <strong>// page title</strong> 'users', <strong>// menu title</strong> 'manage_options', <strong>// capabilities</strong> 'phpcodertech_list',<strong> // menu slug</strong> 'phpcodertech_list' <strong>// function</strong> ); <span style="color: #008000"><strong>// Ssubmenu</strong></span> add_submenu_page('phpcodertech_list', <strong>// parent slug</strong> 'Add New users', <strong>// page title</strong> 'Add New', <strong>// menu title</strong> 'manage_options', <strong>// capability</strong> 'phpcodertech_create', <strong>// menu slug</strong> 'phpcodertech_create' <strong>// function</strong> ); <span style="color: #008000"><strong>//this submenu is HIDDEN, however, we need to add it anyways</strong></span> add_submenu_page(null, <strong>// parent slug</strong> 'Update users', <strong>// page title</strong> 'Update', <strong>// menu title</strong> 'manage_options', <strong>// capability</strong> 'phpcodertech_update', <strong>// menu slug</strong> 'phpcodertech_update' ); <strong>// function</strong> } <span style="color: #008000"><strong>// We now include the functions for the plugin options</strong></span> define('ROOTDIR', plugin_dir_path(__FILE__)); require_once(ROOTDIR . 'list.php'); require_once(ROOTDIR . 'create.php'); require_once(ROOTDIR . 'update.php'); |
From this INIT.PHP file you can see on your admin side bar menu name which you set on this code, look like this,
Step2:
On second step we create code for adding users to database.
create.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 |
<?php <strong><span style="color: #008000">// Create a Users</span></strong> function phpcodertech_create() { if (isset($_POST['insert'])) { <strong><span style="color: #008000">// Get Values</span></strong> echo $id = sanitize_key($_POST['id']); echo $name = sanitize_text_field($_POST['name']); echo $email = sanitize_email($_POST['email']); $msg = ""; <span style="color: #008000"><strong>// Validations</strong></span> if (!preg_match("/^[0-9]*$/",$id) || empty($id)) { $msg = "error:Only numbers allowed in the ID"; } elseif (!preg_match("/^[a-zA-Z ]*$/",$name) or empty($name)) { $msg = "error:Only letters and white space allowed in the name"; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $msg = "error:Invalid email format"; } else { global $wpdb; <strong><span style="color: #008000">// Check if the ID exists</span></strong> $id_check = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM users WHERE id = %d", $id ) ); if ($id_check == 0) { $wpdb->insert( 'users', <span style="color: #008000"><strong>// table</strong></span> array('id' => $id, 'name' => $name, 'email' => $email), /<span style="color: #008000"><strong>/ data</strong></span> array('%d', '%s', '%s') /<span style="color: #008000"><strong>/ data format</strong> <strong>// %s (string), %d (integer) and %f (float)</strong></span> ); <span style="color: #008000"><strong>// This should go away and be treated with an object destructor</strong></span> // or something like that. $id = ""; $name = ""; $email = ""; $msg = "updated:Users saved"; } else { $msg = "error:Duplicated ID, try another"; } } } ?> <link href="<?php echo WP_PLUGIN_URL; ?>/phpcodertech/phpcodertech_style.css" type="text/css" rel="stylesheet" /> <div class="wrap"> <h2>Add New Users</h2> <?php if (!empty($msg)) { $fmsg = explode(':',$msg); echo "<div class=\"{$fmsg[0]}\"><p>{$fmsg[1]}</p></div>"; } ?> <p> <a href="<?php echo admin_url('admin.php?page=phpcodertech_list')?>"> « Back to Userss list</a> </p> <form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>"> <table class='wp-list-table widefat fixed'> <tr> <th>ID</th> <!-- TODO Javascript only numbers validation --> <td><input type="text" name="id" value="<?php echo $id;?>"/> <em>(numbers)</em></td> </tr> <tr> <th>Name</th> <td><input type="text" name="name" value="<?php echo $name;?>"/></td> </tr> <tr> <th>Email</th> <td><input type="text" name="email" value="<?php echo $email;?>"/></td> </tr> </table> <input type="submit" name="insert" value="Save" class="button"> </form> </div> <?php } |
Step3:
on this step we fetch the data from database which we inster using create.php code.
List.php
In this file delete operation code is also available.
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 |
<?php <strong><span style="color: #008000">// List all Userss </span></strong>function phpcodertech_list () { global $wpdb; $msg = ""; /<span style="color: #008000"><strong>/ Deletes a Users</strong></span> if(isset($_GET['delete']) && isset($_GET['id'])) { /<span style="color: #008000"><strong>/ Get Values</strong></span> $id = sanitize_key($_GET['id']); if (!preg_match("/^[0-9]*$/",$id)) $msg = "error:Only numbers allowed in the ID"; else { $wpdb->delete( 'users', array( 'ID' => $id ) ); $msg = "updated:Users deleted!"; } } <span style="color: #008000"><strong>// List all Users</strong></span> $rows = $wpdb->get_results( $wpdb->prepare("SELECT id,name,email from users",$msg) ); ?> <link href="<?php echo WP_PLUGIN_URL; ?>/phpcodertech/phpcodertech_style.css" type="text/css" rel="stylesheet" /> <div class="wrap"> <h2>Userss</h2> <?php if (!empty($msg)) { $fmsg = explode(':',$msg); echo "<div class=\"{$fmsg[0]}\"><p>{$fmsg[1]}</p></div>"; } ?> <a href="<?php echo admin_url('admin.php?page=phpcodertech_create'); ?>">Add New</a> <table class='wp-list-table widefat fixed'> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th> </th> </tr> <?php foreach ($rows as $row ){ ?> <tr> <td><?php echo $row->id ?></td> <td><?php echo $row->name ?></td> <td><?php echo $row->email ?></td> <td> <a href="<?php echo admin_url("admin.php?page=phpcodertech_update&id=".$row->id); ?>">Update</a> | <a href="<?php echo admin_url("admin.php?page=phpcodertech_list&delete&id=".$row->id); ?>" onclick="return confirm('Are you sure?')">Delete</a> </td> </tr> <?php } ?> </table> </div> <?php } |
Step4:
For updation of data we create the update.php file
Update.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 |
<?php function phpcodertech_update () { global $wpdb; <span style="color: #008000"><strong>// Get Values</strong></span> $id = sanitize_key($_GET['id']); $name = sanitize_text_field($_POST['name']); $email = sanitize_email($_POST['email']); $msg = ""; /<strong><span style="color: #008000">/update</span></strong> if(isset($_POST['update'])){ <span style="color: #008000"><strong>// Validations</strong></span> if (!preg_match("/^[0-9]*$/",$id) || empty($id)) { $msg = "error:Only numbers allowed in the ID"; } elseif (!preg_match("/^[a-zA-Z ]*$/",$name) or empty($name)) { $msg = "error:Only letters and white space allowed in the name"; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $msg = "error:Invalid email format"; } else { $wpdb->update( 'users', //table array('name' => $name, 'email' => $email), //data array('ID' => $id ), //where array('%s'), //data format array('%s') //where format ); $msg = "updated:Users updated!"; } } <span style="color: #008000"><strong>// selecting value to update</strong></span> $Userss = $wpdb->get_row( $wpdb->prepare("SELECT id,name,email from users where id=%d",$id) ); ?> <link href="<?php echo WP_PLUGIN_URL; ?>/phpcodertech/phpcodertech_style.css" type="text/css" rel="stylesheet" /> <div class="wrap"> <h2>Update Userss</h2> <?php if (!empty($msg)) { $fmsg = explode(':',$msg); echo "<div class=\"{$fmsg[0]}\"><p>{$fmsg[1]}</p></div>"; } ?> <p> <a href="<?php echo admin_url('admin.php?page=phpcodertech_list')?>"> « Back to Userss list</a> </p> <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <table class='wp-list-table widefat fixed'> <tr> <th>Name</th> <td><input type="text" name="name" value="<?php echo $Userss->name;?>"/></td> </tr> <tr> <th>Email</th> <td><input type="text" name="email" value="<?php echo $Userss->email;?>"/></td> </tr> </table> <input type='submit' name="update" value='Save' class='button'> </form> </div> <?php } |
Where may i find the mentioned stylesheet for the crud?