Skip to content

Instantly share code, notes, and snippets.

@jitenbharadava
Created January 1, 2016 13:06
Show Gist options
  • Save jitenbharadava/0a2e5965b740dceabeb6 to your computer and use it in GitHub Desktop.
Save jitenbharadava/0a2e5965b740dceabeb6 to your computer and use it in GitHub Desktop.
Learn WordPress plugin Development
<?php
/*
Plugin Name: AlphansoTech Plugin
Description: Plugin for testing purpose
Version: 1
Author: AlphansoTech
Author URI: http://alphansotech.com
*/
add_action('admin_menu', 'at_alphansotech_menu');
// returns the root directory path of particular plugin
define('ROOTDIR', plugin_dir_path(__FILE__));
require_once(ROOTDIR . 'employee_list.php');
function at_alphansotech_menu() {
add_menu_page('employee_list', //page title
'Employee Listing', //menu title
'manage_options', //capabilities
'Employee Listing', //menu slug
employee_list //function
);
}
?>
----------------------------------------------------------------------------------
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>Role</th>
<th>Contact</th>
</tr>
</thead>
<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
}
?>
-------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `wp_employee_list` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`role` varchar(100) NOT NULL,
`contact` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `employee_list`
ADD PRIMARY KEY (`id`);
ALTER TABLE `employee_list`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
INSERT INTO `wp-learn`.`employee_list` (`id`, `name`, `role`, `contact`) VALUES (NULL, 'Hardik K. Vyas', 'Php Developer', '+91 9879565656'), (NULL, 'Mark M. Knight', 'Blog Writer', '630-531-9601'), (NULL, 'Annie D. Naccarato', 'Project Leader', '630-531-xxxx'), (NULL, 'Jayesh P. Patel', 'Web Designer', '630-531-xxxx'), (NULL, 'Alvin B. Reddick', 'ifone Developer', '630-531-xxxx');
employee_list.php
<tbody>
<?php
global $wpdb;
$table_name = $wpdb->prefix . 'employee_list';
$employees = $wpdb->get_results("SELECT id,name,contact,role from $table_name");
foreach ($employees as $employee) {
?>
<tr>
<td><?= $employee->id; ?></td>
<td><?= $employee->name; ?></td>
<td><?= $employee->role; ?></td>
<td><?= $employee->contact; ?></td>
</tr>
<?php } ?>
</tbody>
To enable this functionality, we need to write some code in root php file (alphanso_plugin.php )
global $at_db_version;
$at_db_version = '1.0';
function at_datatable() {
global $wpdb;
global $at_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(100) DEFAULT '' NOT NULL,
role(100) DEFAULT '' NOT NULL,
contact(100) DEFAULT '' NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
add_option('at_db_version', $at_db_version);
}
register_activation_hook(__FILE__, 'at_datatable');
employee_list.php
==========================
add_shortcode('short_employee_list', 'employee_list');
More hear link <a href="http://www.alphansotech.com/blogs/learn-wordpress-plugin-development/">http://www.alphansotech.com/blogs/learn-wordpress-plugin-development/</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment