Skip to content

Instantly share code, notes, and snippets.

@tatemz
Last active December 19, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatemz/9b215dcda05dda94526c to your computer and use it in GitHub Desktop.
Save tatemz/9b215dcda05dda94526c to your computer and use it in GitHub Desktop.
WCFay 2013
<?php
class WCFAY_Plugin_Settings {
public function __construct() {
}
public function create_page() {
}
public function register_settings() {
}
public function page_output() {
}
public function section_output() {
}
public function field_output() {
}
public function validate() {
}
}
<?php
public function __construct() {
add_action( 'admin_menu', array( $this, 'create_page' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'update_option_wcfay_plugin_options', array( $this, 'validate') );
}
public function create_page() {
add_options_page(
'WCFay Plugin Page Title',
'WCFay Plugin Menu Title',
'manage_options',
'wcfay_plugin_options_slug',
array( $this, 'page_output' )
);
}
<?php
public function register_settings() {
// Register the database settings key and validation callback
register_setting( 'wcfay_plugin_options', 'wcfay_plugin_options' );
// Create a settings section for the "wcfay_plugin_options_slug" page
add_settings_section(
'wcfay_plugin_section_one_key',
'Section 1 Title',
array( $this, 'section_output' ),
'wcfay_plugin_options_slug'
);
// Add a settings field to that section and "wcfay_plugin_section_one" page
add_settings_field(
'wcfay_plugin_field_one_key',
'Field 1 Title',
array( $this, 'field_output' ),
'wcfay_plugin_options_slug',
'wcfay_plugin_section_one_key',
array(
'type' => 'text',
)
);
}
<?php
public function page_output() {
?>
<div class="wrap">
<h2>WordCamp Fayetteville Plugin Settings</h2>
<form action="options.php" method="post">
<?php settings_fields( 'wcfay_plugin_options' ); ?>
<?php do_settings_sections( 'wcfay_plugin_options_slug' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function section_output( $section ) {
echo '<h4>' . $section['id'] . '\'s description...</h4>';
}
public function field_output( $args ) {
if ( $args['type'] == 'text' ) {
$options = get_option( 'wcfay_plugin_options' );
$option_1 = ( isset( $options['option_1'] ) ) ? $options['option_1'] : '';
printf( '<input type="text" name="wcfay_plugin_options[option_1]" value="%s">', $option_1 );
}
}
<?php
/**
* The short description
*
* As many lines of extendend description as you want
*
* @access public or private
* @author author name <author@email>
* @see name of another element that can be documented,
* produces a link to it in the documentation
* @since a version or a date
* @version version
* @param type [$varname] description
* @return type description
*/
<?php
/*
Plugin Name: WCFay Plugin
Plugin URI: http://anchor.is/presenting
Description: Is anyone ever going to read this?
Version: 1.0
Author: Anchor Studios
Author URI: http://anchor.is
License: GPL2
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
<?php
if ( !class_exists( 'WCFAY_Plugin' ) ) {
class WCFAY_Plugin {
public function __construct() {
}
public function init() {
}
public function activate() {
}
public function deactivate() {
}
public function autoload() {
}
public function includes() {
}
public function actions_and_filters() {
}
}
$GLOBALS['wcfay_plugin'] = new WCFAY_Plugin();
}
<?php
public function __construct() {
// Auto-load classes
if ( function_exists( "__autoload" ) ) {
spl_autoload_register( "__autoload" );
}
spl_autoload_register( array( $this, 'autoload' ) );
// Define version
define( 'WCFAY_PLUGIN_VERSION', $this->version );
// Installation
register_activation_hook( __FILE__, array( $this, 'activate' ) );
// Updates
add_action( 'admin_init', array( $this, 'update' ), 5 );
// Include dependencies
$this->includes();
// Actions & Filters
$this->actions_and_filters();
// Initialize the plugin
add_action( 'init', array( $this, 'init' ) );
// Loaded action
do_action( 'wcfay_plugin_loaded' );
}
<?php
public function init() {
// Before Initialization
do_action( 'before_wcfay_plugin_init' );
// Create options
$this->options = new WCFAY_Plugin_Options;
// Register Taxonomies
$this->register_taxonomies();
// Register Post Types
$this->register_post_types();
// Register Image Sizes
$this->image_sizes();
// After Initialization
do_action( 'wcfay_plugin_init' );
}
<?php
public function plugin_url() {
if ( $this->plugin_url ) return $this->plugin_url;
return $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
}
public function plugin_path() {
if ( $this->plugin_path ) return $this->plugin_path;
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
}
public function autoload( $class ) {
$class = strtolower( $class );
if ( strpos( $class, 'wcfay_plugin' ) === 0 ) {
$path = $this->plugin_path() . '/library/php/classes/';
$file = 'class-' . str_replace( '_', '-', $class ) . '.php';
if ( is_readable( $path . $file ) ) {
include_once( $path . $file );
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment