My first wordpress plugin | how create wordpress plugin | wordpress plugin tutorial
<?php
/*
* Plugin Name: Mydb
* Description: A short example showing how to add a taxonomy called Course.
* Version: 1.0
* Author: developer.wordpress.org
* Author URI: https://wordpress.slack.com/team/aternus
*/
/*
==============STEP 6==================
*/
function set_error($value)
{
echo "<div id='wrap'>
<div class='error'>
".$value."
</div>
</div>";
}
if (isset($_POST['submit']) && isset($_POST['name'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['text']) && !empty($_POST['url']) ) {
Mydb_Insert_data($_POST['name'],$_POST['email'],$_POST['text'],$_POST['url']);
}else{
set_error("all field are required");
}
}
function Mydb_Insert_data($n,$e,$t,$u)
{
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$wpdb->insert(
$table_name,
array(
'name' => $n,
'email' => $e,
'text' => $t,
'url' => $u,
)
);
}
/*
==============STEP 5==================
*/
function Mydb_script_and_style_function()
{
wp_register_style( 'mydb_admin_style_tag', plugins_url('/css/style.css',__FILE__), false );
wp_enqueue_style( 'mydb_admin_style_tag' );
}
add_action( 'admin_enqueue_scripts', "Mydb_script_and_style_function");
/*
==============STEP 4==================
*/
function Mydb_database_register_option( )
{
register_setting("Mydb_register", "name" );
register_setting("Mydb_register", "email" );
register_setting("Mydb_register", "text" );
register_setting("Mydb_register", "url" );
}
/*
==============STEP 3==================
*/
function Mydb_Add_new__page( )
{
?>
<div class="wrap">
<form action="options.php" method="post" id="dataForm">
<?php settings_fields('Mydb_register'); ?>
<?php do_settings_sections('Mydb_register'); ?>
<table id="dataAddnewForm">
<tr>
<td><input type="text" class="mydb-input-class" placeholder="Enter your name" name="name" value="<?php echo get_option('name');?>"></td>
<td><input type="text" class="mydb-input-class" placeholder="Enter your Email" name="email" value="<?php echo get_option('email');?>"> </td>
<td><input type="text" class="mydb-input-class" placeholder="Enter Some Text" name="text" value="<?php echo get_option('text');?>"></td>
<td><input type="text" class="mydb-input-class" placeholder="Url link this text" name="url" value="<?php echo get_option('url');?>"></td>
<td><?php submit_button(); ?></td>
</tr>
</table>
</form>
</div>
<?php
Mydb_home_page();
}
function Mydb_home_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$qry = "SELECT * FROM $table_name ";
$data = $wpdb->get_results( $qry );
?>
<div id="wrap">
<table class="table" width="100%" id="mydb-table" >
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Text</th>
<th>Url</th>
<th>Short code</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key ) {?>
<tr>
<td><?=$key->name?></td>
<td><?=$key->email?></td>
<td><?=$key->text?></td>
<td><?=$key->url?></td>
<td> [mydb id="<?=$key->id?>"]</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
}
function Mydb_menu_page( )
{
add_menu_page( "Mybd all database","Mydb","manage_options","Mydb_menu_slug","Mydb_home_page", "dashicons-grid-view", 6);
add_submenu_page("Mydb_menu_slug","Add New Data","Add New Data", "manage_options", "Mydb_add_new_slug","Mydb_Add_new__page");
add_submenu_page("Mydb_menu_slug","Add Media","Add Media","manage_option","Add_Media_slug","Add_media");
add_action( "admin_init", "Mydb_database_register_option" );
}
add_action("admin_menu","Mydb_menu_page", 10, 1 );
/*
==============STEP 1==================
*/
function Mydb_Install()
{
global $wpdb;
global $mydb;
$table_name = $wpdb->prefix . 'mybd';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id int(9) NOT NULL AUTO_INCREMENT,
name text NOT NULL,
email varchar(100) NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'mydb', $mydb );
}
register_activation_hook(__FILE__,"Mydb_Install");
/*
==============STEP 2==================
*/
function Mydb_data_insert()
{
global $wpdb;
$welcome_name = 'Saddam Hussain';
$welcome_text = 'Congratulations, you just completed the installation!';
$welcome_email="saddam1234321@gmail.com";
$welcome_url="www.iamsaddamhussain.com";
$table_name = $wpdb->prefix . 'mybd';
$wpdb->insert(
$table_name,
array(
'name' => $welcome_name,
'email' => $welcome_email,
'text' => $welcome_text,
'url' => $welcome_url,
)
);
}
register_activation_hook(__FILE__,"Mydb_data_insert");
global $mydb;
add_shortcode("mydb","mybd_shorcode_generate");
function mybd_shorcode_generate($atts ){
if ($atts['id']=="") {
return "<div>Error! Please Enter the id of short code.</div>";
}
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$qry = "SELECT * FROM $table_name where id=".$atts['id']."";
$dat = $wpdb->get_results( $qry );
if (!$dat) {
return "<div>Error! this id is invalid. Please select valid id.</div>";
}
$returnData="";
foreach ($dat as $data) {
$returnData .="<ul>
<li>".$data->name."</li>
<li>".$data->email."</li>
<li>".$data->text."</li>
<li>".$data->url."</li>
</ul>";
}
return $returnData;
}
<?php
/*
* Plugin Name: Mydb
* Description: A short example showing how to add a taxonomy called Course.
* Version: 1.0
* Author: developer.wordpress.org
* Author URI: https://wordpress.slack.com/team/aternus
*/
/*
==============STEP 6==================
*/
function set_error($value)
{
echo "<div id='wrap'>
<div class='error'>
".$value."
</div>
</div>";
}
if (isset($_POST['submit']) && isset($_POST['name'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['text']) && !empty($_POST['url']) ) {
Mydb_Insert_data($_POST['name'],$_POST['email'],$_POST['text'],$_POST['url']);
}else{
set_error("all field are required");
}
}
function Mydb_Insert_data($n,$e,$t,$u)
{
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$wpdb->insert(
$table_name,
array(
'name' => $n,
'email' => $e,
'text' => $t,
'url' => $u,
)
);
}
/*
==============STEP 5==================
*/
function Mydb_script_and_style_function()
{
wp_register_style( 'mydb_admin_style_tag', plugins_url('/css/style.css',__FILE__), false );
wp_enqueue_style( 'mydb_admin_style_tag' );
}
add_action( 'admin_enqueue_scripts', "Mydb_script_and_style_function");
/*
==============STEP 4==================
*/
function Mydb_database_register_option( )
{
register_setting("Mydb_register", "name" );
register_setting("Mydb_register", "email" );
register_setting("Mydb_register", "text" );
register_setting("Mydb_register", "url" );
}
/*
==============STEP 3==================
*/
function Mydb_Add_new__page( )
{
?>
<div class="wrap">
<form action="options.php" method="post" id="dataForm">
<?php settings_fields('Mydb_register'); ?>
<?php do_settings_sections('Mydb_register'); ?>
<table id="dataAddnewForm">
<tr>
<td><input type="text" class="mydb-input-class" placeholder="Enter your name" name="name" value="<?php echo get_option('name');?>"></td>
<td><input type="text" class="mydb-input-class" placeholder="Enter your Email" name="email" value="<?php echo get_option('email');?>"> </td>
<td><input type="text" class="mydb-input-class" placeholder="Enter Some Text" name="text" value="<?php echo get_option('text');?>"></td>
<td><input type="text" class="mydb-input-class" placeholder="Url link this text" name="url" value="<?php echo get_option('url');?>"></td>
<td><?php submit_button(); ?></td>
</tr>
</table>
</form>
</div>
<?php
Mydb_home_page();
}
function Mydb_home_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$qry = "SELECT * FROM $table_name ";
$data = $wpdb->get_results( $qry );
?>
<div id="wrap">
<table class="table" width="100%" id="mydb-table" >
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Text</th>
<th>Url</th>
<th>Short code</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key ) {?>
<tr>
<td><?=$key->name?></td>
<td><?=$key->email?></td>
<td><?=$key->text?></td>
<td><?=$key->url?></td>
<td> [mydb id="<?=$key->id?>"]</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
}
function Mydb_menu_page( )
{
add_menu_page( "Mybd all database","Mydb","manage_options","Mydb_menu_slug","Mydb_home_page", "dashicons-grid-view", 6);
add_submenu_page("Mydb_menu_slug","Add New Data","Add New Data", "manage_options", "Mydb_add_new_slug","Mydb_Add_new__page");
add_submenu_page("Mydb_menu_slug","Add Media","Add Media","manage_option","Add_Media_slug","Add_media");
add_action( "admin_init", "Mydb_database_register_option" );
}
add_action("admin_menu","Mydb_menu_page", 10, 1 );
/*
==============STEP 1==================
*/
function Mydb_Install()
{
global $wpdb;
global $mydb;
$table_name = $wpdb->prefix . 'mybd';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id int(9) NOT NULL AUTO_INCREMENT,
name text NOT NULL,
email varchar(100) NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'mydb', $mydb );
}
register_activation_hook(__FILE__,"Mydb_Install");
/*
==============STEP 2==================
*/
function Mydb_data_insert()
{
global $wpdb;
$welcome_name = 'Saddam Hussain';
$welcome_text = 'Congratulations, you just completed the installation!';
$welcome_email="saddam1234321@gmail.com";
$welcome_url="www.iamsaddamhussain.com";
$table_name = $wpdb->prefix . 'mybd';
$wpdb->insert(
$table_name,
array(
'name' => $welcome_name,
'email' => $welcome_email,
'text' => $welcome_text,
'url' => $welcome_url,
)
);
}
register_activation_hook(__FILE__,"Mydb_data_insert");
global $mydb;
add_shortcode("mydb","mybd_shorcode_generate");
function mybd_shorcode_generate($atts ){
if ($atts['id']=="") {
return "<div>Error! Please Enter the id of short code.</div>";
}
global $wpdb;
$table_name = $wpdb->prefix . 'mybd';
$qry = "SELECT * FROM $table_name where id=".$atts['id']."";
$dat = $wpdb->get_results( $qry );
if (!$dat) {
return "<div>Error! this id is invalid. Please select valid id.</div>";
}
$returnData="";
foreach ($dat as $data) {
$returnData .="<ul>
<li>".$data->name."</li>
<li>".$data->email."</li>
<li>".$data->text."</li>
<li>".$data->url."</li>
</ul>";
}
return $returnData;
}
Comments
Post a Comment