Skip to content

Instantly share code, notes, and snippets.

@admataz
Created February 14, 2013 15:13
Show Gist options
  • Save admataz/4953447 to your computer and use it in GitHub Desktop.
Save admataz/4953447 to your computer and use it in GitHub Desktop.
WordPress Plugin Boilerplate
<?php
/*
Plugin Name: Plugin Boilerplate
Version: 0.1-alpha
Description: This is how I like to make sense of the WordPress plugin architecture
Author: Adam Davis
Author URI: http://admataz.com
Plugin URI: http://admataz.com
Text Domain: admataz-plugin-boilerplate
Domain Path: /languages
*/
new PluginClassName();
class PluginClassName {
var $db_version = 1;
var $option_name = '_PLUGIN_SLUG_';
var $options = array();
function __construct() {
$this->options = get_option( $this->option_name );
register_activation_hook( __FILE__, array( &$this, 'activate' ) );
register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );
$this->actions_and_filters();
}
/**
* add custom items to the admin menu
*/
function admin_menu() {
/*
add_menu_page("Africa Check Website", "Africa Check Website", 'manage_options', 'africa_check_site',array(&$this,'site_options_page'));
add_submenu_page('africa_check_site','User Suggested Claims', 'Public suggested claims', 'manage_options', 'africa_check_suggested_claims', array(&$this,'suggested_claims_data'));
add_submenu_page('africa_check_site','Comment options', 'Comment Options', 'manage_options', 'africa_check_comment_options', array(&$this,'comment_options'));
*/
}
/**
* collect all actions and filters in one place
* @return [type] [description]
*/
function actions_and_filters() {
add_action( 'init', array( &$this, 'init' ) );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
//add_action( 'admin_menu', array(&$this,'admin_menu'));
//add_action( 'save_post', array(&$this,'save_post'));
//add_action( 'delete_post', array(&$this,'delete_post'));
// add_filter('preprocess_comment', array(&$this, 'validate_comment'), 9, 2);
// add_action( 'wp_insert_comment', array(&$this, 'insert_comment'), 10, 2);
}
/**
* Run this on every page load
*
* @return [type] [description]
*/
function init() {
$this->create_taxonomy();
$this->custom_post_types();
$this->custom_fields();
$this->enqueue_scripts();
}
/**
* javascripts to be used by this plugin
*/
function enqueue_scripts() {
// we don't generally want the scripts added to the admin interface
if ( !is_admin() ) {
// wp_enqueue_script('foundation', plugins_url( 'js/script.js' , __FILE__ ), array('jquery'), '1.0', TRUE);
}
}
/**
* do stuff when the admin interface is loaded…
*/
function admin_init() {
$this->check_for_upgrades();
$this->setup_options();
}
/**
* Stuff to do when this plugin is activated
*
*
*/
function activate() {
/*
global $wpdb;
$table_name = $wpdb->prefix . "posts";
$case_studies_page = get_page_by_path( 'case-studies' );
$sql = "UPDATE `$table_name` SET post_type='story' WHERE post_type='page' and post_parent=".$case_studies_page->ID;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
*/
}
/**
* Stuff to do when this plugin is deactivated (cleanup and leave it as you found it)
*/
function deactivate() {
/*
global $wpdb;
$table_name = $wpdb->prefix . "posts";
$case_studies_page = get_page_by_path( 'case-studies' );
$sql = "UPDATE `$table_name` SET post_type='page', post_parent=".$case_studies_page->ID." WHERE post_type='story'";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
*/
}
/**
* Register custom post types here
*/
function custom_post_types() {
/*
register_post_type( 'story', array(
'label'=>"Stories",
'supports'=>array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'comments', 'author' ),
'public'=>TRUE,
'taxonomies'=>array( 'post_tag' )
) );
*/
}
/**
* Add custom taxonomies here
*/
function create_taxonomy() {
/*
$structure_args = array(
'label' => 'Site Structure tags',
'show_tagcloud' => TRUE,
'public' => TRUE,
'hierarchical' => TRUE);
register_taxonomy('site_structure', 'content_blocks', $structure_args);
$countries_args = array(
'label' => 'Countries',
'show_tagcloud' => TRUE,
'public' => TRUE,
'hierarchical' => FALSE);
register_taxonomy('countries', array('reports','post','in_the_news'), $countries_args);
*/
}
/**
* method to set up the custom fields - using the developer custom fields plugin
* use Steve's plugin to define custom fields - reference is here:
* http://sltaylor.co.uk/wordpress/plugins/slt-custom-fields/docs/
*
*/
function custom_fields() {
if ( function_exists( 'slt_cf_register_box' ) ) {
/*
slt_cf_register_box( array(
'type' => 'post',
'id'=>'story_fields',
'title'=>'Featured',
'context' => 'above-content',
'description'=>'Control which stories are featured',
'fields' => array(
array(
'name' => 'story_featured',
'label'=> 'Featured on home page',
'type'=> 'checkbox',
'scope'=>array('story'),
)
)
)
);
// */
}
}
/**
* do something every time a post is saved (action callback)
*/
function save_post( $post_id ) {
/*
$the_post = get_post($post_id);
if($the_post->post_type == 'page'){
$this->generate_structure_terms($post_id);
}
*/
}
/**
* do something every time a post is deleted (action callback)
*/
function delete_post( $post_id ) {
/*
$the_post = get_post($post_id);
if($the_post->post_type == 'page'){
$this->delete_structure_terms($post_id);
}
*/
}
/**
* register custom options for admin pages
* WP 2.9+ has this really verbose, but they say, safe way of generating options pages…
* All fields need to be whitelisted, and then can be processed in a callback
*
*/
private function setup_options() {
/*
$o=get_option('africa_check_auto_generated_page_ids');
if(!empty($o)) {
register_setting('africa_check_comment_options', 'comment_terms_text', array( &$this, 'admin_set_comments_terms'));
add_settings_section('africa_check_site_comment_options', 'Comments Options', array(&$this,'comments_options'), 'africa_check_comment_options' );
add_settings_field( 'comment_terms_text','Terms and Conditions for comment contributions', array(&$this,'comments_options_terms_text'), 'africa_check_comment_options', 'africa_check_site_comment_options' );
register_setting( 'africa_check_site_options', 'africa_check_clear_pages' , array( &$this, 'admin_clear_generated_pages' ) );
add_settings_section('africa_check_site_options_setup', 'Setup Options', array(&$this,'settings_section_cb'), 'africa_check_site_options');
add_settings_field( 'africa_check_clear_pages', 'Remove Generated Pages', array( &$this, 'settings_field_cb' ), 'africa_check_site_options', 'africa_check_site_options_setup' );
} else {
register_setting( 'africa_check_site_options', 'africa_check_create_pages' , array( &$this, 'admin_create_generated_pages' ) );
add_settings_section('africa_check_site_options_setup', 'Setup Options', array(&$this,'settings_section_cb'), 'africa_check_site_options');
add_settings_field( 'africa_check_create_pages', 'Generate Default Content', array( &$this, 'settings_field_cb' ), 'africa_check_site_options', 'africa_check_site_options_setup' );
}
*/
}
/**
* do upgrades…
*
*/
function upgrade( $current_db_version ) {
// update to version 1...
if ( $current_db_version < 1 ) {
error_log( 'do upgrade to v1' );
}
}
/**
* do upgrade if there is no db_version or the db_version is old
*/
function check_for_upgrades() {
if ( false === $this->options || ! isset( $this->options['db_version'] ) || $this->options['db_version'] < $this->db_version ) {
if ( ! is_array( $this->options ) ) {
$this->options = array();
}
$current_db_version = isset( $this->options['db_version'] ) ? $this->options['db_version'] : 0;
$this->upgrade( $current_db_version );
$this->options['db_version'] = $this->db_version;
update_option( $this->option_name, $this->options );
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment