<?php
/*
Plugin Name: CRUD Operations
Plugin URI: Description: A simple plugin that allows you to perform Create (INSERT), Read (SELECT), Update and Delete operations.
Version: 1.0.0
Author: Author URI: License: GPL2
*/
register_activation_hook( __FILE__, 'crudOperationsTable');
define( 'CRUD__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function crudOperationsTable() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'userstable';
$sql = "CREATE TABLE `$table_name` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(220) DEFAULT NULL,
`email` varchar(220) DEFAULT NULL,
`image` varchar(220) DEFAULT NULL,
PRIMARY KEY(user_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
";
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
add_action('admin_menu', 'addAdminPageContent');
function addAdminPageContent() {
add_menu_page('CRUD', 'CRUD', 'manage_options' ,__FILE__, 'crudAdminPage', 'dashicons-wordpress');
}
function crudAdminPage() {
global $wpdb;
$table_name = $wpdb->prefix . 'userstable';
if (isset($_POST['newsubmit'])) {
$name = $_POST['newname'];
$email = $_POST['newemail'];
$target_dir = CRUD__PLUGIN_DIR."img/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//$new_filename = $target_dir . uniqid() . '.' . $imageFileType;
$new_filename = uniqid() . '.' . $imageFileType;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_filename))
{
$filee = $new_filename;
}
$wpdb->query("INSERT INTO $table_name(name,email, image) VALUES('$name','$email','$filee')");
//echo "<script>location.replace('admin.php?page=crud.php');</script>";
echo "<script>location.replace('admin.php?page=crud-operations/crud-operations.php');</script>";
}
if (isset($_POST['uptsubmit'])) {
$id = $_POST['uptid'];
$name = $_POST['uptname'];
$email = $_POST['uptemail'];
$target_dir = CRUD__PLUGIN_DIR."img/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//$new_filename = $target_dir . uniqid() . '.' . $imageFileType;
$new_filename = uniqid() . '.' . $imageFileType;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_filename))
{
$filee = $new_filename;
}
$wpdb->query("UPDATE $table_name SET name='$name',email='$email',image='$filee' WHERE user_id='$id'");
//echo "<script>location.replace('admin.php?page=crud.php');</script>";
echo "<script>location.replace('admin.php?page=crud-operations/crud-operations.php');</script>";
}
if (isset($_GET['del'])) {
$del_id = $_GET['del'];
$wpdb->query("DELETE FROM $table_name WHERE user_id='$del_id'");
echo "<script>location.replace('admin.php?page=crud-operations/crud-operations.php');</script>";
}
if (!isset($_GET['upt'])) {
?>
<div class="wrap">
<h2>CRUD Operations</h2>
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th>User ID</th>
<th>UserName</th>
<th>Email Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<form action="" method="post" enctype='multipart/form-data'>
<tr>
<td><input type="text" value="AUTO_GENERATED" disabled></td>
<td><input type="text" id="newname" name="newname"></td>
<td><input type="text" id="newemail" name="newemail"></td>
<td><input type="file" id="fileToUpload" name="fileToUpload"></td>
<td><button id="newsubmit" name="newsubmit" type="submit">INSERT</button></td>
</tr>
</form>
<?php
$result = $wpdb->get_results("SELECT * FROM $table_name");
foreach ($result as $print) {
echo "
<tr>
<td>$print->user_id</td>
<td>$print->name</td>
<td>$print->email</td>
<td><img style='width: 150px' src=".$print->image."></td>
<td><a href='admin.php?page=crud-operations/crud-operations.php&upt=$print->user_id'><button type='button'>UPDATE</button></a> <a href='admin.php?page=crud-operations/crud-operations.php&del=$print->user_id'><button type='button'>DELETE</button></a></td>
</tr>
";
}
?>
</tbody>
</table>
<br>
<br>
<?php
}
if (isset($_GET['upt'])) {
$upt_id = $_GET['upt'];
$result = $wpdb->get_results("SELECT * FROM $table_name WHERE user_id='$upt_id'");
foreach($result as $print) {
$name = $print->name;
$email = $print->email;
}
echo "<br><br>
<table class='wp-list-table widefat striped'>
<thead>
<tr>
<th width='25%'>User ID</th>
<th width='25%'>UserName</th>
<th width='25%'>Email Address</th>
<th width='25%'>Actions</th>
</tr>
</thead>
<tbody>
<form action='' method='post' enctype='multipart/form-data'>
<tr>
<td width='25%'>$print->user_id <input type='hidden' id='uptid' name='uptid' value='$print->user_id'></td>
<td width='25%'><input type='text' id='uptname' name='uptname' value='$print->name'></td>
<td width='25%'><input type='text' id='uptemail' name='uptemail' value='$print->email'></td>
<td width='25%'><input type='file' id='fileToUpload' name='fileToUpload'><br><img style='width: 200px' src=".$print->image."></td>
<td width='25%'><button id='uptsubmit' name='uptsubmit' type='submit'>UPDATE</button> <a href='admin.php?page=crud-operations/crud-operations.php'><button type='button'>CANCEL</button></a></td>
</tr>
</form>
</tbody>
</table>";
}
?>
</div>
<?php
}
just add--- add_post_type_support( 'page', 'excerpt' ); ----to functions.php
Comments
Post a Comment