Skip to content

Instantly share code, notes, and snippets.

@khromov
Created January 12, 2015 00:18
Show Gist options
  • Save khromov/245eb158a08c7467b291 to your computer and use it in GitHub Desktop.
Save khromov/245eb158a08c7467b291 to your computer and use it in GitHub Desktop.
WordPress Object-oriented plugin skeleton - http://snippets.khromov.se/a-nicer-way-to-structure-your-plugin/
<?php
/*
Plugin Name: Your Plugin
Plugin URI: http://snippets.khromov.se/a-nicer-way-to-structure-your-plugin/
Description: Your plugin skeleton
Version: 1.0
Author: you
Author URI: http://your-site.com
License: GPL2
*/
/* The class encapsulates the plugin and its functions */
class Your_Plugin
{
/* Put any plugin variables here */
var $plugin_variable;
/* The constructor will be used to setup actions and filters */
function __construct()
{
/**
* Set a plugin variable
*/
$this->plugin_variable = 'test';
/**
* Hook on plugins_loaded. This is the first hook available to regular plugins.
* It happens when all plugins have been initialized and is the best place to put your logic.
*/
add_action('plugins_loaded', array($this, 'plugin_loaded'));
/* Optionally, add additional hooks here. */
}
/**
* Put your main plugin logic here!
*/
function plugin_loaded()
{
/* Here is how to call a function inside your class: */
$sum = $this->add(1,2);
}
/**
* A plugin function that adds
* two numbers together and
* returns them.
*
* @param $var1
* @param $var2
* @return mixed
*/
function add($var1, $var2)
{
return $var1+$var2;
}
}
/* This initiates your plugin */
$your_plugin = new Your_Plugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment