Here are some basic steps to create a plugin called contact_us, that will shows data in admin panel.
1-Create a folder contact_us in wp-content/plugins
2-Create a file contact_us.php and open it. Now you need to put some basic plugin details and remember wordpress will recognize you by this information.
3-Now go to the admin and click Plugins. You will see your plugin is in the listing.
/*
Plugin Name:Contact Us Plugin
Plugin URI: http://www.91weblessons.com
Description: WordPress Contact Us plugin
Author: Vishal Gupta
Version: 1.0.1
Author URI: http://www.91weblessons.com
*/
4-Now the time to set database table setting , which will created dynamically at time of plugin installation. And also dropped at uninstallation.
5-Now time to set up functions for plugin installation and uninstallation.
global $wpdb;
$pro_table_prefix=$wpdb->prefix;
define('PRO_TABLE_PREFIX', $pro_table_prefix);
6-Now save the code and go to the admin and click Plugins and activate Contact Us plugin and go to phpmyadmin and your table now list here.
register_activation_hook(__FILE__,'contact_us_install');
register_deactivation_hook(__FILE__ , 'contact_us_uninstall' );
add_action('admin_menu','contact_us_admin_menu');
// It will tells to wordpress, calls contact_us_admin_menu
after plugin installation function
function contact_us_install()
{
global $wpdb;
$table = PRO_TABLE_PREFIX."contact_us";
$structure = "CREATE TABLE $table (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(80) NOT NULL,
email VARCHAR(50) NOT NULL,
UNIQUE KEY id (id)
);";
$wpdb->query($structure);
}
function contact_us_uninstall()
{
global $wpdb;
$table = PRO_TABLE_PREFIX."contact_us";
$structure = "drop table if exists $table";
$wpdb->query($structure);
}
7-Now time to set admin section menu.
8-Now create a file called contact_us_admin_listing.php into same folder and put data display code into the file.
function contact_us_admin_menu() {
add_menu_page("Contact Us","Contact Us",8,__FILE__,"contact_us_admin_menu_list");
}
// It will tells to wordpress, to shows Contact Us link in admin panel and
whenever any one click into it call contact_us_admin_menu_list function.
function contact_us_admin_menu_list() {
include 'contact_us_admin_listing.php';
}
And here is the output.
<?php
$columns = array(
'Name' => 'Name',
'Email' => 'Email'
);
register_column_headers('pro-list-site', $columns);
global $wpdb;
$sql = "SELECT *FROM ".PRO_TABLE_PREFIX."contact_us where 1";
$results = $wpdb->get_results($sql);
?>
<div class="wrap">
<?php echo "<h2>" . __( 'Contact Us Data' ) . "</h2>"; ?>
<table class="widefat page fixed" cellspacing="0">
<thead>
<tr>
<?php print_column_headers('pro-list-site'); ?>
</tr>
</thead>
<tfoot>
<tr>
<?php print_column_headers('pro-list-site', false); ?>
</tr>
</tfoot>
<tbody>
<?php
if(count($results) > 0)
{
foreach($results as $result)
{
echo "<tr><td>".$result-> name."</td><td>".$result->email."</td></tr>";
}
}else{
echo '<tr><td colspan="2">No Data Found</td></tr>';
}
?>
No Data Found because right now contact_us table is blank. Just go to phpmyadmin and add some data into table and you will see that data here.
9-Using add_shortcode function you can shows plugin into front end. That I will explain you in my next tutorial.
Comments