Plugin Name: My Plugin
Plugin URI: https://developer.wordpress.org/
Description: A brief description
of my plugin
Version: 1.0
Author: Ankit Shah
Author URI: http://itnewcode.blogspot.in/
License: GPLv2
*/
/*** Plugin Activation Code ***/
function aks_activate_plugin(){
/* Create Table */
global
$wpdb;
$charset_collate
= $wpdb->get_charset_collate();
$table_name
= $wpdb->prefix . 'new_register_test';
$sql =
"CREATE TABLE $table_name (
id
mediumint(9) NOT NULL AUTO_INCREMENT,
name
varchar(30) NOT NULL,
email
varchar(30) NOT NULL,
gender
varchar(6) NOT NULL,
regtime
datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
views
smallint(5) NOT NULL,
clicks
smallint(5) NOT NULL,
UNIQUE
KEY id (id)
)
$charset_collate;";
require_once(
ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta(
$sql );
}
register_activation_hook( __FILE__, 'aks_activate_plugin'
);
/*** Add wordpress Menu ***/
add_action('admin_menu', 'aks_reg_menu_page');
function aks_reg_menu_page()
{
/*
add_menu_page()
function arguments:
1.
Page title – used in the title tag of the page (shown in the browser bar) when
it is displayed.
2.
Menu title – used in the menu on the left.
3.
Capability – the user level allowed to access the page.
4.
Menu slug – the slug used for the page in the URL.
5.
Function – the name of the function you will be using to output the content of
the page.
6.
Icon – A url to an image or a Dashicons string.
7.
Position – The position of your item within the whole menu.
*/
add_menu_page('Register
Plugin Settings', 'Register Settings', 'administrator', __FILE__, 'aks_reg_form' , plugins_url('/images/icon.png',
__FILE__) );
}
function aks_reg_form(){
$empty_error
= $success_msg = " ";
if(isset($_POST['aks-register'])){
if(empty($_POST['aks_name_field'])
|| empty($_POST['aks_email_field']) || empty($_POST['aks_gender_field'])){
$empty_error
= "Please fill all fields";
}
else
{
$aks_name_field=$_POST['aks_name_field'];
$aks_email_field=$_POST['aks_email_field'];
$aks_gender_field=$_POST['aks_gender_field'];
/*
Insert Query */
global
$wpdb;
$charset_collate
= $wpdb->get_charset_collate();
$table_name
= $wpdb->prefix . 'new_register_test';
$insertsql="INSERT
INTO `$table_name`(`id`, `name`, `email`, `gender`) VALUES (' ',
'$aks_name_field', '$aks_email_field', '$aks_gender_field')";
require_once(
ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta(
$insertsql );
$success_msg="Succesfully
Registed";
}
}
?>
Register here,
/*** Plugin Uninstall Code ***/
function aks_uninstall_plugin(){
/* Delete Query */
global
$wpdb;
$charset_collate
= $wpdb->get_charset_collate();
$table_name
= $wpdb->prefix . 'new_register_test';
$deletesql
= "DROP TABLE IF EXISTS '$table_name';";
require_once(
ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta(
$deletesql );
}
register_uninstall_hook(__FILE__, 'aks_uninstall_plugin');
?>
For more Interesting, Useful Article & codes visit IT New Code.
0 comments:
Post a Comment