let’s create WordPress plugin the easy way:
01. Create plug-in template as follows:
- Browse to http://wppb.me/ and generate plugin, this is Wordpress boilerplate generator and generates OOP plugin.
- Download the generated plug-in.
- Install it.
- In general here is a link to wordpress developer resources.
02. plugin code
- Search plugin directory for string “public static function activate()“.
- activation: in the activation phase we will install and create the wp_db table
- find the “public static function activate()” and add code to create the db table
-
public static function activate() { /**Create an instance of the database class**/ if (!isset($wpdb)) $wpdb = $GLOBALS['wpdb']; /**Set the custom table **/$table_name = $wpdb->prefix . "crud_links"; $demo_browser_db_version = '1.0'; /**Execute the sql statement to create or update the custom table**/ if ( $wpdb->get_var("show tables like '$table_name'" ) != $table_name ) { $sql = "CREATE TABLE " . $table_name . " ( ID int(11) NOT NULL AUTO_INCREMENT, Name varchar(255) DEFAULT NULL, Link varchar(255) DEFAULT NULL, LinkOrder int(11) NOT NULL, Description varchar(2255) DEFAULT NULL, OpenWindow tinyint(1) NOT NULL, PRIMARY KEY (ID) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); add_option( "my_plugin_db_version", '1.0' ); } }
- uninstall.php – when we remove the plugin this code will run
-
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { exit;global $wpdb; $table_name = $wpdb->prefix . "crud_links"; $sql = "DROP TABLE IF EXISTS $table_name;"; $wpdb->query($sql); delete_option("my_plugin_db_version"); }
- wp hooks – admin menu
- Search plug-in directory for string private function define_admin_hooks().
- Add to this function the following line of code:
$this->loader->add_action( ‘admin_menu’, $plugin_admin, ‘admin_menu’ );
- this will call the function in admin folder
- Add the following code to php file under admin directory, not the index.php file, this will create admin page:
public function admin_menu() { add_menu_page("CRUD Operation", "Links Crud", 1, "admin_linkesmenu", array($this,"admin_links_display")); }
function admin_links_display() { include('partials/general-links-admin-display.php'); }
- Php file under Directory Admin\Partials, general-links-admin-display.php, Insert all html + php code and CRUD action you need for back-end.
<? php /** * Provide a dashboard view for the plugin * * This file is used to markup the public-facing aspects of the plugin. * * @link: http://www.flash-jet.com * @since 1.0.0 * * @package General_Links * @subpackage General_Links/admin/partials */ ?> <!-- This file should primarily consist of HTML with a little bit of PHP. --> <?php global $wpdb; /**Set the custom table **/ $table_name = $wpdb->prefix . "crud"; $CN =mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Sorry Connection Problem"); $DB =mysql_select_db(DB_NAME,$CN) or die("Sorry Not Connect to Your Database"); if(isset($_POST['add'])) { $name=$_POST['name']; $link=$_POST['link']; $order=$_POST['order']; $description=$_POST['description']; $openwindow=$_POST['openwindow']; $sql="INSERT INTO wp_crud_links (`Name`, `Link`, `LinkOrder`, `Description`, `OpenWindow`) VALUES ('".$name."','".$link."','".$order."','".$description."','".$openwindow."')"; $res=mysql_query($sql); if($res) { echo "<script>document.location='admin.php?page=admin_linkesmenu&msg=1'</script>"; } else { echo "Error !"; } } if(isset($_POST['Update'])) { $uid=$_GET['id']; $name=$_POST['name']; $link=$_POST['link']; $order=$_POST['order']; $description=$_POST['description']; $openwindow=$_POST['openwindow']; $sql="update wp_crud_links set Name='".$name."' , Address='".address."' , Mobile='".$mobile."' , Fileupload='".$image_name."' where ID='".$uid."'"; $reg=mysql_query($sql); if($reg) { echo"<script>document.location='admin.php?page=admin_linkesmenu&msg=2'</script>"; } else { echo"Error !"; } } if($_GET['action'] == 'delete') { $did=$_GET['id']; $sql1="delete from wp_crud_links where ID='".$did."'"; $res1=mysql_query($sql1); if($res1) { echo"<script>document.location='admin.php?page=admin_linkesmenu&msg=3'</script>"; } else { echo "Error !"; } } ?> <?php if(isset($_GET['action']) && ($_GET['action'] == 'edit')) { $urid=$_GET['id']; $sql="select * from wp_crud_links where ID='".$urid."'"; $reg=mysql_query($sql); $res=mysql_fetch_array($reg); $uname= $res['Name']; $ulink= $res['Link']; $uorder= $res['LinkOrder']; $udescription= $res['Description']; $uopenwindow= $res['OpenWindow']; ?> <form name="add" action="" method="post"> <table cellpadding="5" cellspacing="5"> <tr> <td>Name :</td> <td><input type="text" name="name" value="<?php echo $uname; ?>" /></td></tr> </tr> <tr> <td>Link :</td> <td><input type="text" name="link" value="<?php echo $ulink; ?>" /></td></tr> </tr> <tr> <td>Order :</td> <td><input type="text" name="order" value="<?php echo $uorder; ?>" /></td></tr> </tr> <tr> <td>Description :</td> <td><textarea name="description" rows="3" cols="50"> <?php echo $udescription; ?></textarea></td></tr> </tr> <tr> <td>Open in new window :</td> <td><input type="checkbox" name="openwindow" value="<?php echo $uopenwindow; ?>" <?php echo ($uopenwindow == 1 ? 'checked' : '') ?> /></td></tr> </tr> <td><input type="submit" name="Update" value="update" align="middle"/> <a href="admin.php?page=admin_linkesmenu" style="text-decoration:none;"><input type="button" name="cancel" value="Cancel" id="html-upload" align="middle"></a> </td> </tr> </table> </form> <?php }elseif(isset($_GET['action']) && ($_GET['action'] == 'add')) { ?> <hr> <form name="add" action="" method="post"> <table cellpadding="5" cellspacing="5"> <tr> <td>Name :</td> <td><input type="text" name="name" /></td></tr> </tr> <tr> <td>Link :</td> <td><input type="text" name="link" /></td></tr> </tr> <tr> <td>Order :</td> <td><input type="text" name="order" /></td></tr> </tr> <tr> <td>Description :</td> <td><textarea name="description" rows="3" cols="50"></textarea></td></tr> </tr> <tr> <td>Open in new window :</td> <td><input type="checkbox" name="openwindow" value="1" /></td></tr> </tr> <tr> <td> <input type="submit" name="add" value="Add" align="middle"/> <a href="admin.php?page=admin_linkesmenu" style="text-decoration:none;"><input type="button" name="cancel" value="Cancel" id="html-upload" align="middle"></a> </td> </tr> </table> </form> <?php } else { ?> <?php if($_GET['msg'] == 1) { ?> <h2><center> Record Sucessfull Added</center></h2> <?php } ?> <?php if($_GET['msg'] == 2) { ?> <h2><center> Record Sucessfull Updated</center></h2> <?php } ?> <?php if ($_GET['mag'] == 3){?> <h2><center> Record Sucessfull Deleted</center></h2> <?php } ?> <div class="wrap" style="float:none;"> <h2>Simple CRUD Operation <a class="add-new-h2" href="admin.php?page=admin_linkesmenu&action=add">Add New</a></h2> </div> <table cellspacing="0" class="wp-list-table widefat fixed pages"> <thead> <tr> <th style="width:30px;" class="" id="" scope="col"><span>No</span></th> <th style="width:100px;" class="" id="" scope="col"><span>Name</span></th> <th style="width:100px;" class="" id="" scope="col"><span>Link</span></th> <th style="width:100px;" class="" id="" scope="col"><span>Order</span></th> <th style="width:200px;" class="" id="" scope="col"><span>Description</span></th> <th style="width:200px;" class="" id="" scope="col"><span>OpenWindow</span></th> <th style="width:200px;" class="" id="" scope="col"><span></span></th> </tr> </thead> <tbody id="the-list"> <?php $i = 1; $sqlg = "SELECT * FROM wp_crud_links "; //$resg = $wpdb->get_results($sqlg); //foreach ( $resg as $rowg ) $resg = mysql_query($sqlg); if($resg === FALSE) { die(mysql_error()); } while($rowg = mysql_fetch_array($resg)) { $FileUpload=$rowg['FileUpload']; $rid=$rowg['ID']; echo '<tr valign="top">'; echo '<td>'.$i.'</td>'; echo '<td>'.$rowg['Name'].'</td>'; echo '<td>'.$rowg['Link'].'</td>'; echo '<td>'.$rowg['LinkOrder'].'</td>'; echo '<td>'.$rowg['Description'].'</td>'; echo '<td><input type="checkbox" '.($rowg['OpenWindow'] == 1?'checked':'').'/></td>'; echo '<td><a href="admin.php?page=admin_linkesmenu&action=edit&id='.$rid.'" >Edit</a> || <a href="admin.php?page=admin_linkesmenu&action=delete&id='.$rid.'" onclick="javascript:return confirm(\'Are You Sure?\')">Delete</a></td>'; echo '</tr>'; $i++; } ?> </tbody> </table> <?php } ?>
- let’s create the front end to display the data from the DB
- public php in wp is done via shortcodes
- private function define_public_hooks() add new line
-
$this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
- this will call register_shortcodes function in the public folder of the plugin
-
public function register_shortcodes() { add_shortcode( 'crud_links_operation', array( $this, 'shortcode_crud_links_operation' ) ); }
- this will call shortcode_crud_links_operation that will generate html to display in the shortcode
- after we create the shortcode we can insert it to a post or or page
- [crud_links_operation][/crud_links_operation] and go to wp post and she the html you inserted in the short code.
3 responses to “WordPress plugin development OOP way”
great ooP thanks!.
WordPress must separate model view and controller, this echo ‘is html? wtF’ it’s horrible
It seems like it needs more touch-ups
Great post
Thank you