Saturday, December 10, 2011

How to create Wordpress admin Plugin

First Create a folder name--- baw_create_menu in Plugins folder
Then Create                     --- baw_create_menu.php


<?php
session_start();


 /*
        Plugin Name: baw_create_menu
        Plugin URI: http://wordpress.org/#
        Description: created by manoj.
        Author: manu


       */
      
       $sql = "CREATE TABLE IF NOT EXISTS `deal` (
  `id` int(11) NOT NULL auto_increment,
  `store` varchar(50) NOT NULL,
  `url` varchar(50) NOT NULL,
  `deal` varchar(50) NOT NULL,
  `code` varchar(50) NOT NULL,
  `start` varchar(50) NOT NULL,
  `end` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ";
 mysql_query($sql);   
      
$result = mysql_query("SELECT * FROM deal")
                or die(mysql_error()); 

           
               
add_action('admin_menu', 'baw_create_menu');
function baw_create_menu() {
    //create new top-level menu
    add_menu_page('ADD A Deal Form', 'Add A Deal', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__));
    //call register settings function
    add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
    //register our settings
    register_setting( 'baw-settings-group', 'new_option_name' );
    register_setting( 'baw-settings-group', 'some_other_option' );
    register_setting( 'baw-settings-group', 'option_etc' );
}

function baw_settings_page() {
?>
<script type="text/javascript">
function validat()
{

var theurl1=document.formtest.url.value;

var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
if(document.formtest.store.value=="")
    {
        alert("Please Enter Your Store Name");
        document.formtest.store.focus();
        return false;
    }
   
    else if(!tomatch.test(theurl1))
    {
        alert("Please Enter a Valid URL- http://www.google.com");
        document.formtest.url.focus();
        return false;
    }


else if(document.formtest.deal.value=="")
    {
        alert("Please Enter Your Deal");
            document.formtest.deal.focus();
        return false;
    }
   
    else if(document.formtest.code.value=="")
    {
        alert("Please Enter Your Code");
            document.formtest.code.focus();
        return false;
    }
   
    else if(document.formtest.start.value=="")
    {
        alert("Please Enter Your Deal Start Date");
            document.formtest.start.focus();
        return false;
    }
   
    else if(document.formtest.end.value=="")
    {
        alert("Please Enter Your Deal End Date");
            document.formtest.end.focus();
        return false;
    }
   
   
       
        return true;
       
   
}
</script>
<div class="wrap">
<h2>Add a Deal:</h2>
<form method="post" action="" name="formtest" id="formtest" style="margin:0px; padding:0px;"  onsubmit="return validat()">
    <?php settings_fields( 'baw-settings-group' ); ?>
  
    <table border="1" cellpadding="0" cellspacing="0">
        <tr valign="top">
        <th><h4>Store*</h4></th> <th scope="row"><h4>URL</h4></th> <th scope="row"><h4>Deal</h4></th>
       
        </tr>
        
        <tr valign="top">
       <td><input type="text" name="store" value="<?php echo get_option('store'); ?>" id="store"/></td>
        <td><input type="text" name="url" value="<?php echo get_option('url'); ?>" id="url" /></td>
         <td><input type="text" name="deal" value="<?php echo get_option('deal'); ?>" id="deal" /></td>
        </tr>
       
       
         <tr valign="top">
        <th scope="row">Code</th>
         <th scope="row">Start</th>
           <th scope="row">End</th>
      
        </tr>
         <tr valign="top">
        <td><input type="text" name="code" value="<?php echo get_option('code'); ?>" id="code" /></td>
        <td><input type="text" name="start" value="<?php echo get_option('start'); ?>"  id="start" /></td>
        <td><input type="text" name="end" value="<?php echo get_option('end'); ?>"  id="end" /></td>
        </tr>
        <tr valign="top"></tr>
        <tr valign="top">
        <td></td>
        <td><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>"  name="submit"/></td>
        <td> <input name="" type="reset" /><?=$msg?></td>
        </tr>
    </table>
   
    <p class="submit">
<?php
   
    if(isset($_POST['submit']))
{
    $store=$_POST['store'];
$url=$_POST['url'];
$deal=$_POST['deal'];
$code=$_POST['code'];
$start=$_POST['start'];
$end=$_POST['end'];

mysql_query("insert into deal values('','$store','$url','$deal','$code','$start','$end')");
$_SESSION['msg']="Successfully data Inserted";   
   
}
   
   
     ?>
    
    </p>
</form>
<table>
<tr><?php
/*
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
      
       
        // number of results to show per page
        $per_page = 5;
       
        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM deal");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['no']) && is_numeric($_GET['no']))
        {
                $show_page = $_GET['no'];
               
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page;
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page;
                }              
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page;
        }
       
        // display pagination
       
        echo "<p><b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='http://localhost/tuneblog/wp-admin/admin.php?page=baw_create_menu/baw_create_menu.php&no=$i'>$i</a> ";
        }
        echo "</p>";
           
        // display data in table
       
        echo "<table  width='100%' border='1' cellspacing='0' cellpadding='2'>";
       echo "<tr>
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Store</th>
      
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Deal</th>
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Code</th>
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Start</th>
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>End</th>
       </tr>";
        // loop through results of database query, displaying them in the table
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
       
                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><a href="' . mysql_result($result, $i, 'url') . '">' . mysql_result($result, $i, 'store') . '</a></td>';
                
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'deal') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'code') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'start') . '</td>';
                  echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'end') . '</td>';
           
                echo "</tr>";
        }
        // close table>
        echo "</table>";
       
        // pagination
       
?>
</tr>

</table>






</div>
<?php } ?>

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts