Skip to main content

My first wordpress plugin | how create wordpress plugin | wordpress plugin tutorial

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;
}

Comments

Popular posts from this blog

Minimum and maximum number with - and + 500 in Mysql and Sql query and Find out table scheema of our database in Mysql and sql query and Find out all table name of our database in Mysql and sql query and Find out current date and time in mysql and sql help of sql query

Minimum and maximum number with - and + 500 in Mysql and Sql query:- syntax:-  SELECT MIN(Used)-500 , max(Used)+500 FROM audit Find out current date and time in mysql and sql help of sql query:- syntax:- SELECT now() Find out database name in Mysql and sql query:- syntax:-  SELECT DATABASE(); Find out all table name of our database in Mysql and sql query:- syntax:- SHOW TABLES; Find out   table scheema   of our database in Mysql and sql query:- syntax:- DESCRIBE <table name>;

Php prime number program with use of class

Php prime number  program with use of class <?php class find_p_number { # Function to check single number is Prime or not static function isPrime ( $ number ) { # Start temporary number from 2 (1 is not a prime number) $ i = 2 ; # 1 is not a prime number if ( $ number == 1 ) return false ; # 2 is the only even prime number if ( $ number == 2 ) return true ; # Check numbers other than 1 and 2 while ( $ i <= sqrt ( $ number ) ) { if ( $ number % $ i == 0 ) return false ;   $ i ++ ; } # If the execution has come to this point, means it is a prime Number :) return true ; } # Function to check Prime Numbers between two given numbers static function betweenTwoNumbers ( $ num1 , $ num2 ) { $ prime_numbers = array ( ) ; # Check Prime Numbers between two given numbers for ( $ num1 ; $ num1 < $ num2 ; $ num1 +...