wordpress plugin – Network site database

I have never coded a plugin for WordPress and this is my first try. I have made themes and used third-party plugins and edited them too. However this is a totally new ball game.

I want to be able to do one of the following – whichever is best.

Read More
  1. 1 table – all networking sites have their own data but it’s all stored in the one table.
CREATE TABLE myplugin (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
networkid INT(30) NOT NULL,
datereserved DATE NOT NULL,
timeslot TIME NOT NULL,
bookedby VARCHAR(60),
datetimebookedon TIMESTAMP,
paidstatus INT(1) NOT NULL,
paidstripid VARCHAR(40)
)

Or should I split this into their own tables, and if so how?

Here is the code, but it’s not what I want. I thought this was meant to be easy not rocket science – I’d like to see a simple plugin that outlines “hello world” in a plugin format that will work for both single and network sites with db creation. The hour-long videos are making no sense.

<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
 * Plugin Name: Book Online
 * Plugin URI: http://harrower.xyz
 * Description: This plugin allows your customers to schedual a time to meet with you and pay any fee that you require.
 * Version: 1.0.0
 * Author: Harrower.xyz
 * Author URI: http://harrower.xyz
 * License: GPL2
 */
 /**
  * Start the plugin only if in Admin side and if site is Multisite
  */
 if( is_admin() && is_multisite() )
 {
     add_action(
         'plugins_loaded',
         array ( book_online_multisite::get_instance(), 'plugin_setup' )
     );
 }

 class book_online_multisite
 {
     protected static $instance = NULL;
     public $blogs = array();
     public $plugin_url = '';
     public $plugin_path = '';

     public static function get_instance()
     {
         NULL === self::$instance and self::$instance = new self;
         return self::$instance;
     }

     /**
      * Plugin URL and Path work as normal
      */
     public function plugin_setup()
     {
         $this->plugin_url    = plugins_url( '/', __FILE__ );
         $this->plugin_path   = plugin_dir_path( __FILE__ );
         add_action(
             'book-online-dashboard.php',
             array( $this, 'load_blogs' )
         );
         add_action( 'admin_menu', 'register_my_custom_menu_page' );
     }


     // this is the menu area
     function register_my_custom_menu_page() {

     add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );


     }





/// function to create the DB / Options / Defaults
 function book_online_options_install() {
    global $wpdb;
    global $bonlinedb;

    $book_online_caldb = $wpdb->prefix . "booking_online_calendar";
    $book_online_customersdb = $wpdb->prefix . "booking_online_customers";

    // create the ECPT metabox database table
    if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
    {
    //$bonlinedb = $wpdb->prefix . 'bookonline';
        $sql = "CREATE TABLE  `$book_online_customersdb` (
        `id` INT() NOT NULL AUTO_INCREMENT,
        `booking_date`DATE NOT NULL,
        UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }


  // create the ECPT metabox database table
    if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
    {
        $sql = "CREATE TABLE `$book_online_caldb` (
        `id` INT() NOT NULL AUTO_INCREMENT,
    `bid` INT() NOT NULL,
    `booking_time` TIME NOT NULL,
    `status` INT() NOT NULL DEFAULT  '0';
        UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

  // this is the menu area
  function register_my_custom_menu_page() {

  add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
  add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
      'manage_options', 'my-top-level-slug');
  add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
      'manage_options', 'my-secondary-slug');

  }


  add_action( 'admin_menu', 'register_my_custom_menu_page' );
/*



     public function __construct() {}

     public function load_blogs()
     {
         /**
          * Using "is_network" property from $current_screen global variable.
          * Run only in /wp-admin/network/plugins.php
          */
         global $current_screen;
         if( !$current_screen->is_network )
             return;

          /*
          *A couple of Multisite-only filter hooks and a regular one.
          */
        add_action(
                 'network_admin_plugin_action_links',
                 array( $this, 'list_plugins' ),
                 10, 4
        );
        * add_filter(
        *         'views_plugins-network', // 'views_{$current_screen->id}'
        *         array( $this, 'inactive_views' ),
        *         10, 1
         *);
         *add_action(
        *         'admin_print_scripts',
      *           array( $this, 'enqueue')
      *   );
         */
         /**
          * This query is quite frequent to retrieve all blog IDs.
          */
         global $wpdb;
         $this->blogs = $wpdb->get_results(
                 " SELECT blog_id, domain
                 FROM {$wpdb->blogs}
                 WHERE site_id = '{$wpdb->siteid}'
                 AND spam = '0'
                 AND deleted = '0'
                 AND archived = '0' "
         );
     }

     /**
      * Enqueue script and style normally.
      */
     public function enqueue()
     {
         wp_enqueue_script(
                 'ndbae-js',
                 $this->plugin_url . 'core/ndbae.js',
                 array(),
                 false,
                 true
         );
         wp_enqueue_style(
                 'main',
                 $this->plugin_url . 'core/main.css'
         );
     }

     /**
      * Check if plugin is active in any blog
      * Using Multisite function get_blog_option
      */
     private function get_network_plugins_active( $plug )
     {
         $active_in_blogs = array();
         foreach( $this->blogs as $blog )
         {
             $the_plugs = get_blog_option( $blog['blog_id'], 'active_plugins' );
             foreach( $the_plugs as $value )
             {
                 if( $value == $plug )
                     $active_in_blogs[] = $blog['domain'];
             }
         }
         return $active_in_blogs;
     }
 }


class book_online_single_site{



 // function to create the DB / Options / Defaults
 function book_online_options_install() {
        global $wpdb;
    global $bonlinedb;

    $book_online_caldb = $wpdb->prefix . "booking_online_calendar";
    $book_online_customersdb = $wpdb->prefix . "booking_online_customers";

    // create the ECPT metabox database table
    if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
    {
    //$bonlinedb = $wpdb->prefix . 'bookonline';
        $sql = "CREATE TABLE  `$book_online_customersdb` (
        `id` INT() NOT NULL AUTO_INCREMENT,
        `booking_date`DATE NOT NULL,
        UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }


  // create the ECPT metabox database table
    if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
    {
        $sql = "CREATE TABLE `$book_online_caldb` (
        `id` INT() NOT NULL AUTO_INCREMENT,
    `bid` INT() NOT NULL,
    `booking_time` TIME NOT NULL,
    `status` INT() NOT NULL DEFAULT  '0';
        UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

  // this is the menu area
  function register_my_custom_menu_page() {

  add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
  add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
      'manage_options', 'my-top-level-slug');
  add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
      'manage_options', 'my-secondary-slug');

  }


  add_action( 'admin_menu', 'register_my_custom_menu_page' );


  add_action( 'wp_head', 'book_online' );
    function book_online() {
      echo 'I am in the head section';
    }

    add_shortcode( 'bookonline', 'book_online_live' );
    function book_online_live() {
      return 'this is a test';
    }

    // run the install scripts upon plugin activation
    register_activation_hook(__FILE__,'book_online_options_install');

 }



}
 ?>

Related posts