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

how send mail using Swift mailer | switmailer send mail tutorial

how send mail using Swift mailer | switmailer send mail tutorial  <?php /* step 1 include Swift mailer file ;  */ include("vendor/swiftmailer/swiftmailer/lib/swift_required.php"); /* step 2 create your user details */ $senderEmail="saddam1234321@gmail.com"; $senderPassword="****Your password****"; $senderName="Saddam huassin"; /* step 3 set SMTP host  for sending mail  */ $user = Swift_SmtpTransport::newInstance('smtp.gmail.com','587','tls'); /* step 4 set your user details  */ $user->setUsername($senderEmail); $user->setPassword($senderPassword); /* step 5 create swift  mailer instance with a variable */ $mailer = Swift_Mailer::newInstance($user); /* step 6 Create a message */ $message = Swift_Message::newInstance('Wonderful Subject')   ->setFrom(array($senderEmail => $senderName))   ->setTo(array($senderEmail))   ->setBody('Here is the message itsel...

how install Swift mailer in localhost | swift mailer installation | composer install swift mailer

how install Swift mailer in localhost | swift mailer installation | composer install swift mailer Step 1:- open cmd  Step 2;- copy this command " composer required swiftmailer/swiftmailer @stable " . Step 3;- paste in cmd and press enter. Step 4;- after few second swift mailer ready to use .................................