Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created December 14, 2011 01:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefrosty/1474715 to your computer and use it in GitHub Desktop.
Save thefrosty/1474715 to your computer and use it in GitHub Desktop.
Core functionality plugin.
<?php
/**
* Plugin Name: PluginName
* Plugin URI: http://austinpassy.com/wordpress-plugins/
* Description: Core functionality for __________
* Version: 1.0.0
* Author: Austin Passy
* Author URI: http://austinpassy.com/
*
* @copyright 2012
* @author Austin Passy
* @link http://austinpassy.com/2012
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package Plugability
*/
/* Initiate the plugin */
add_action( 'plugins_loaded', create_function( '', 'new PluginName();' ) );
// TODO: rename this class to a proper name for yuour plugin
class PluginName {
/**
* TODO: update these values to reflect the name and slug of your plugin.
*/
const name = 'Plugin Name';
const slug = 'plugin-name-slug';
const version = '1.0.0';
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*/
function __construct() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_uninstall_hook( __FILE__, array( $this, 'deactivate' ) );
if ( !load_plugin_textdomain( self::slug, '/wp-content/languages/' ) )
load_plugin_textdomain( self::slug, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Define constants used throughout the plugin
$this->constants();
// Load JavaScript and Stylesheets
$this->register_scripts_and_styles();
/*
* TODO:
* Define the custom functionality for your plugin. The first parameter of the
* add_action/add_filter calls are the hooks into which your code should fire.
*
* The second parameter is the function name located within this class. See the stubs
* later in the file.
*
* For more information:
* http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
*/
add_action( 'init', array( $this, 'actions' ) );
add_filter( 'init', array( $this, 'filters' ) );
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'register_taxonomy' ) );
} // end constructor
/**
* Note: Install the plugin on activation.
*
*/
function install() {
$this->register_post_type();
$this->register_taxonomy();
flush_rewrite_rules();
} // end action_method_name
/**
* This function is executed when the plugin is deactivated.
*
* @return void
*/
function deactivate() {
flush_rewrite_rules();
}
/**
* Note: Actions are points in the execution of a page or process
* lifecycle that WordPress fires.
*
* WordPress Actions: http://codex.wordpress.org/Plugin_API#Actions
* Action Reference: http://codex.wordpress.org/Plugin_API/Action_Reference
*
*/
function actions() {
// TODO define your action method here
} // end actions
/**
* Note: Filters are points of execution in which WordPress modifies data
* before saving it or sending it to the browser.
*
* WordPress Filters: http://codex.wordpress.org/Plugin_API#Filters
* Filter Reference: http://codex.wordpress.org/Plugin_API/Filter_Reference
*
*/
function filters() {
// TODO define your filter method here
} // end filters
/**
* Initializes any custom post types
*
* WordPress codex: http://codex.wordpress.org/Function_Reference/register_post_type
*/
private function register_post_type() {
} // end register_post_types
/**
* Initializes any custom post types
*
* WordPress codex: http://codex.wordpress.org/Function_Reference/register_taxonomy
*/
private function register_taxonomy() {
} // end register_taxonomies
/**
* Initializes constants used for convenience throughout
* the plugin.
*/
private function constants() {
/* TODO
*
* Define this as the name of your plugin.
*/
if ( !defined( 'PLUGINNAME' ) )
define( 'PLUGINNAME', self::name );
/* TODO
*
* this is the slug of your plugin used in initializing it with
* the WordPress API.
* This should also be the
* directory in which your plugin resides. Use hyphens.
*
* For example: wordpress-widget-boilerplate
*/
if ( !defined( 'PLUGINSLUG' ) )
define( 'PLUGINSLUG', self::slug );
} // end constants
/**
* Registers and enqueues stylesheets for the administration panel and the
* public facing site.
*/
private function register_scripts_and_styles() {
if ( is_admin() ) {
$this->load_file( self::slug . '-admin-script', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'js/admin.js', true );
$this->load_file( self::slug . '-admin-style', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'css/admin.css' );
} else {
$this->load_file( self::slug . '-script', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'js/functions.js', true );
$this->load_file( self::slug . '-style', trailingslashit( plugin_dir_path( __FILE__ ) ) . 'css/style.css' );
} // end if/else
} // end register_scripts_and_styles
/**
* Helper function for registering and loading scripts and styles.
*
* @name The ID to register with WordPress
* @file_path The path to the actual file
* @is_script Optional argument for if the incoming file_path is a JavaScript source file.
*/
private function load_file( $name, $file_path, $is_script = false ) {
$url = WP_PLUGIN_URL . $file_path;
$file = WP_PLUGIN_DIR . $file_path;
if ( file_exists( $file ) ) {
if ( $is_script ) {
wp_register_script( $name, $url );
wp_enqueue_scripts( $name );
} else {
wp_register_style( $name, $url );
wp_enqueue_scripts( $name );
} // end if
} // end if
} // end load_file
}; // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment