Learn how to create a Custom CRUD operation (Insert, Update, Delete, View) Plugin in Wordpress From Scratch with Tables created on plugin activation hook of WordPress.
Github:
https://github.com/sahilgulati007/crud-plugin-wordpress
plugin Basic Video:
https://youtu.be/i5Up4mJB7fw
File to create plugin and add menu to wordpress backend
Crud-plugin.php
<?php
/**
* Plugin Name: CRUD Plugin
* Plugin URI: https://example.com/plugins/the-basics/
* Description: Custom Plugin to exceute crud opreation in Wordpress.
* Version: 1.00
* Requires at least: 5.0
* Requires PHP: 7.0
* Author: Sahil Gulati
* Author URI: https://facebook.com/sahilgulati007
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: crud-plugin
*/
//creating database on plugin activation
global $jal_db_version;
$jal_db_version = '1.0';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . 'employee_list';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
address text NOT NULL,
role text NOT NULL,
contact bigint(12),
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );
//adding in menu
add_action('admin_menu', 'at_try_menu');
function at_try_menu() {
//adding plugin in menu
add_menu_page('employee_list', //page title
'Employee Listing', //menu title
'manage_options', //capabilities
'Employee_Listing', //menu slug
employee_list //function
);
//adding submenu to a menu
add_submenu_page('Employee_Listing',//parent page slug
'employee_insert',//page title
'Employee Insert',//menu titel
'manage_options',//manage optios
'Employee_Insert',//slug
'employee_insert'//function
);
add_submenu_page( null,//parent page slug
'employee_update',//$page_title
'Employee Update',// $menu_title
'manage_options',// $capability
'Employee_Update',// $menu_slug,
'employee_update'// $function
);
add_submenu_page( null,//parent page slug
'employee_delete',//$page_title
'Employee Delete',// $menu_title
'manage_options',// $capability
'Employee_Delete',// $menu_slug,
'employee_delete'// $function
);
}
// returns the root directory path of particular plugin
define('ROOTDIR', plugin_dir_path(__FILE__));
require_once(ROOTDIR . 'employee_list.php');
require_once (ROOTDIR.'employee_insert.php');
require_once (ROOTDIR.'employee_update.php');
require_once (ROOTDIR.'employee_delete.php');
?>
Employee_list.php
<?php
function employee_list() {
?>
<style>
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 20px;
text-align: center;
}
</style>
<div class="wrap">
<table>
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
<th>Role</th>
<th>Contact</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
global $wpdb;
$table_name = $wpdb->prefix . 'employee_list';
$employees = $wpdb->get_results("SELECT id,name,address,contact,role from $table_name");
foreach ($employees as $employee) {
?>
<tr>
<td><?= $employee->id; ?></td>
<td><?= $employee->name; ?></td>
<td><?= $employee->address; ?></td>
<td><?= $employee->role; ?></td>
<td><?= $employee->contact; ?></td>
<td><a href="<?php echo admin_url('admin.php?page=Employee_Update&id=' . $employee->id); ?>">Update</a> </td>
<td><a href="<?php echo admin_url('admin.php?page=Employee_Delete&id=' . $employee->id); ?>"> Delete</a></td>
</tr>
<?php } ?>
</tbody>
<!--<tbody>
<tr>
<td>1</td>
<td>Hardik K. Vyas</td>
<td>Php Developer</td>
<td>+91 940894578</td>
</tr>
<tr>
<td>2</td>
<td>Mark M. Knight</td>
<td>Blog Writer</td>
<td>630-531-9601</td>
</tr>
<tr>
<td>3</td>
<td>Annie D. Naccarato</td>
<td>Project Leader</td>
<td>144-54-XXXX</td>
</tr>
<tr>
<td>4</td>
<td>Jayesh P. Patel</td>
<td>Web Designer</td>
<td>+91 98562315</td>
</tr>
<tr>
<td>5</td>
<td>Alvin B. Reddick</td>
<td>ifone Developer</td>
<td>619-11-XXXX</td>
</tr>
</tbody>-->
</table>
</div>
<?php
}
add_shortcode('short_employee_list', 'employee_list');
?>
Employee_insert.php
<?php
function employee_insert()
{
//echo "insert page";
?>
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<form name="frm" action="#" method="post">
<tr>
<td>Name:</td>
<td><input type="text" name="nm"></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="adrs"></td>
</tr>
<tr>
<td>Desigination:</td>
<td><select name="des">
<option value="developer">Developer</option>
<option value="designer">Designer</option>
</select>
</td>
</tr>
<tr>
<td>Mob no:</td>
<td><input type="number" name="mob"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Insert" name="ins"></td>
</tr>
</form>
</tbody>
</table>
<?php
if(isset($_POST['ins'])){
global $wpdb;
$nm=$_POST['nm'];
$ad=$_POST['adrs'];
$d=$_POST['des'];
$m=$_POST['mob'];
$table_name = $wpdb->prefix . 'employee_list';
$wpdb->insert(
$table_name,
array(
'name' => $nm,
'address' => $ad,
'role' => $d,
'contact'=>$m
)
);
echo "inserted";
// wp_redirect( admin_url('admin.php?page=page=Employee_List'),301 );
//header("location:http://localhost/wordpressmyplugin/wordpress/wp-admin/admin.php?page=Employee_Listing");
// header("http://google.com");
?>
<meta http-equiv="refresh" content="1; url=http://localhost/wordpressmyplugin/wordpress/wp-admin/admin.php?page=Employee_Listing" />
<?php
exit;
}
}
?>
Employee_update.php
<?php
//echo "update page";
function employee_update(){
//echo "update page in";
$i=$_GET['id'];
global $wpdb;
$table_name = $wpdb->prefix . 'employee_list';
$employees = $wpdb->get_results("SELECT id,name,address,contact,role from $table_name where id=$i");
echo $employees[0]->id;
?>
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<form name="frm" action="#" method="post">
<input type="hidden" name="id" value="<?= $employees[0]->id; ?>">
<tr>
<td>Name:</td>
<td><input type="text" name="nm" value="<?= $employees[0]->name; ?>"></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="adrs" value="<?= $employees[0]->address; ?>"></td>
</tr>
<tr>
<td>Desigination:</td>
<td><select name="des">
<option value="developer" <?php if($employees[0]->role=="developer"){echo "selected";} ?> >Developer</option>
<option value="designer" <?php if($employees[0]->role=="designer"){echo "selected";} ?> >Designer</option>
</select>
</td>
</tr>
<tr>
<td>Mob no:</td>
<td><input type="number" name="mob" value="<?= $employees[0]->contact; ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update" name="upd"></td>
</tr>
</form>
</tbody>
</table>
<?php
}
if(isset($_POST['upd']))
{
global $wpdb;
$table_name=$wpdb->prefix.'employee_list';
$id=$_POST['id'];
$nm=$_POST['nm'];
$ad=$_POST['adrs'];
$d=$_POST['des'];
$m=$_POST['mob'];
$wpdb->update(
$table_name,
array(
'name'=>$nm,
'address'=>$ad,
'role'=>$d,
'contact'=>$m
),
array(
'id'=>$id
)
);
$url=admin_url('admin.php?page=Employee_List');
//header("location:http://localhost/wordpressmyplugin/wordpress/wp-admin/admin.php?page=Employee_Listing");
}
?>
Employee_Delete.php
<?php
//echo "employee delete";
function employee_delete(){
echo "employee delete";
if(isset($_GET['id'])){
global $wpdb;
$table_name=$wpdb->prefix.'employee_list';
$i=$_GET['id'];
$wpdb->delete(
$table_name,
array('id'=>$i)
);
echo "deleted";
}
echo get_site_url() .'/wp-admin/admin.php?page=Employee_List';
?>
<!-- <meta http-equiv="refresh" content="0; url=http://localhost/wordpressmyplugin/wordpress/wp-admin/admin.php?page=Employee_Listing" />
--> <?php
//wp_redirect( admin_url('admin.php?page=page=Employee_List'),301 );
//exit;
//header("location:http://localhost/wordpressmyplugin/wordpress/wp-admin/admin.php?page=Employee_Listing");
}
?>
#wordpressPluginDevelopment
#CrudPlugin
#TableCreationOn Activation
Nice
ReplyDeletethis is very nice plugin
ReplyDeletei want plugin for product all type
ReplyDeleteplz expalin functions and code
ReplyDeleteHow to use shortcode on frontend to display table
ReplyDelete