Pages

Friday, December 7, 2012

Basic requirements to create a plugin in wordpress

To create a plugin in wordpress we need to create a folder and php file in it with same name and place it under WP-content/plugins directory

Then in the file add the following comments that describe the plugin

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>
 
The minimum information WordPress needs to recognize your Plugin is the Plugin Name line.

To add admin menu side use following hook:

add_action('admin_menu', 'gvp_add_menu_page');
function gvp_add_menu_page() {
    add_menu_page("ORDERS TILL NOW", "View Orders", "activate_plugins", "view_orders", "orders_admin_page");
}

function orders_admin_page()
{
    include_once dirname(__FILE__) . '/adminend.php';
}


To perform actions at initialization use :

register_activation_hook(__FILE__, 'initialize_me');

function initialize_me() {
    global $wpdb;
    $jsrm_menu_table = $wpdb->prefix . "jsrm_menus";
    $jsrm_item_table = $wpdb->prefix . "jsrm_items";
    $gav_order_table = $wpdb->prefix . "gav_orders";
    $gav_order_items_table = $wpdb->prefix . "gav_orders_items";
    $gav_users_table= $wpdb->prefix . "gav_users";
   
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    if (!empty($wpdb->charset))
        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    if (!empty($wpdb->collate))
        $charset_collate .= " COLLATE $wpdb->collate";

    if ($wpdb->get_var("SHOW TABLES LIKE $gav_users_table") != $gav_users_table) {
        $sql1 = "CREATE TABLE IF NOT EXISTS $gav_users_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                fname text NOT NULL,
                lname text NOT NULL,
                address text NOT NULL,
                                email text NOT NULL,
                phone varchar(255) NOT NULL,
                ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                UNIQUE KEY id (id)
                ) $charset_collate;";
        dbDelta($sql1);
    }
   
     if ($wpdb->get_var("SHOW TABLES LIKE $gav_order_table") != $gav_order_table) {

        $sql3 = "CREATE TABLE IF NOT EXISTS $gav_order_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                custid mediumint(9) NOT NULL,
                                status tinyint(1) NOT NULL,
                UNIQUE KEY id (id),
                                CONSTRAINT `OrderOfCustomer` FOREIGN KEY (`custid`) REFERENCES `$gav_users_table` (`id`)
                ) $charset_collate;";
        dbDelta($sql3);
    }

    if ($wpdb->get_var("SHOW TABLES LIKE $gav_order_items_table") != $gav_order_items_table) {

        $sql2 = "CREATE TABLE IF NOT EXISTS $gav_order_items_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                orderid mediumint(9) NOT NULL,
                itemid mediumint(9) NOT NULL,
                                qty mediumint(9) NOT NULL,
                                amt int(15) NOT NULL,
                UNIQUE KEY id (id),
                                CONSTRAINT `ItemsofOrder` FOREIGN KEY (`orderid`) REFERENCES `$gav_order_table` (`id`)
                ) $charset_collate;";
        dbDelta($sql2);
    }
   
    add_option("gav_order_version", $xap_db_version);
}

To add Shortcode: 

add_shortcode('gav-show-menu', 'gav_showmenu');
function gav_showmenu($atts) {
}

To add stylesheet:
function gav_stylesheet() {
    $gavStyleUrl = WP_PLUGIN_URL . '/gav-retail-order/css/main-stylesheet.css';
    $gavStyleFile = WP_PLUGIN_DIR . '/gav-retail-order/css/main-stylesheet.css';
    //echo file_exists($gavStyleUrl);
    if (file_exists($gavStyleFile)) {
        wp_register_style('gavStyleSheet', $gavStyleUrl);
        wp_enqueue_style('gavStyleSheet');
    }
}

add_action('wp_print_styles', 'gav_stylesheet');

No comments:

Post a Comment