Advertisement
grappler

plugin-boilerplate.php

May 11th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.76 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: TODO
  4. Plugin URI: TODO
  5. Description: TODO
  6. Version: 1.0.0
  7. Author: TODO
  8. Author URI: TODO
  9. Author Email: TODO
  10. Text Domain: plugin-name-locale
  11. License: GNU General Public License Unversioned
  12. License URI: http://www.gnu.org/licenses/gpl.txt
  13.  
  14. Copyright 2013 TODO (email@domain.com)
  15. */
  16.  
  17. /**
  18.  * If this file is attempted to be accessed directly, we'll exit.
  19.  *
  20.  * The following check provides a level of security from other files
  21.  * that request data directly.
  22.  */
  23. if ( ! defined( 'ABSPATH' ) ) {
  24.     exit;
  25. }
  26.  
  27. /*
  28.  * The following constant is used to define a constant for this plugin to make it
  29.  * easier to provide cache-busting functionality on loading stylesheets
  30.  * and JavaScript.
  31.  *
  32.  * After you've defined these constants, do a find/replace on the constants
  33.  * used throughout the rest of this file.
  34.  */
  35. // TODO: Replace 'PLUGIN_NAME' wih the name of your class
  36. if ( ! defined('PLUGIN_NAME_VERSION' ) ) {
  37.  
  38.     // TODO: Make sure that this version correspondings to the value in the 'Version' in the header
  39.     define( 'PLUGIN_NAME_VERSION', '1.0.0' );
  40.  
  41. }
  42.  
  43. /**
  44.  * TODO:
  45.  *
  46.  * Rename this class to a proper name for your plugin. Give a proper description of
  47.  * the plugin, it's purpose, and any dependencies it has.
  48.  *
  49.  * Use PHPDoc tags if you wish to be able to document the code using a documentation
  50.  * generator.
  51.  *
  52.  * @package    PluginName
  53.  * @version    1.0.0
  54.  */
  55. class PluginName {
  56.  
  57.     /**
  58.      * Refers to a single instance of this class.
  59.      *
  60.      * @var    object
  61.      */
  62.     protected static $instance = null;
  63.  
  64.     /**
  65.      * Refers to the slug of the plugin screen.
  66.      *
  67.      * @var    string
  68.      */
  69.     protected $plugin_screen_slug = null;
  70.    
  71.     /**
  72.      * Creates or returns an instance of this class.
  73.      *
  74.      * @since     1.0.0
  75.      * @return    PluginName    A single instance of this class.
  76.      */
  77.     public function get_instance() {
  78.  
  79.         // If the single instance hasn't been set, set it now.
  80.         if ( null == self::$instance ) {
  81.             self::$instance = new self;
  82.         }
  83.  
  84.         return self::$instance;
  85.  
  86.     }
  87.  
  88.     /**
  89.      * Initializes the plugin by setting localization, filters, and administration functions.
  90.      *
  91.      * @since    1.0.0
  92.      */
  93.     private function __construct() {
  94.  
  95.         // Load plugin text domain
  96.         add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
  97.  
  98.         /*
  99.          * Add the options page and menu item.
  100.          * Uncomment the following line to enable the Settings Page for the plugin:
  101.          */
  102.         //add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
  103.  
  104.         /*
  105.          * Register admin styles and scripts
  106.          * If the Settings page has been activated using the above hook, the scripts and styles
  107.          * will only be loaded on the settings page. If not, they will be loaded for all
  108.          * admin pages.
  109.          *
  110.          * add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) );
  111.          * add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
  112.          */
  113.  
  114.         // Register site stylesheets and JavaScript
  115.         add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
  116.         add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_scripts' ) );
  117.  
  118.         // Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
  119.         register_activation_hook( __FILE__, array( $this, 'activate' ) );
  120.         register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
  121.  
  122.         /*
  123.          * TODO:
  124.          *
  125.          * Define the custom functionality for your plugin. The first parameter of the
  126.          * add_action/add_filter calls are the hooks into which your code should fire.
  127.          *
  128.          * The second parameter is the function name located within this class. See the stubs
  129.          * later in the file.
  130.          *
  131.          * For more information:
  132.          * http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
  133.          */
  134.         add_action( 'TODO', array( $this, 'action_method_name' ) );
  135.         add_filter(' TODO', array( $this, 'filter_method_name' ) );
  136.        
  137.     }
  138.  
  139.     /**
  140.      * Fired when the plugin is activated.
  141.      *
  142.      * @param    boolean    $network_wide    True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
  143.      */
  144.     public function activate( $network_wide ) {
  145.         // TODO:    Define activation functionality here
  146.     }
  147.  
  148.     /**
  149.      * Fired when the plugin is deactivated.
  150.      *
  151.      * @param    boolean    $network_wide    True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
  152.      * @since    1.0.0
  153.      */
  154.     public function deactivate( $network_wide ) {
  155.         // TODO:    Define deactivation functionality here
  156.     }
  157.  
  158.     /**
  159.      * Loads the plugin text domain for translation
  160.      */
  161.     public function load_plugin_textdomain() {
  162.  
  163.         // TODO: replace "plugin-name-locale" with a unique value for your plugin
  164.         $domain = 'plugin-name-locale';
  165.         $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  166.  
  167.         load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
  168.         load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
  169.        
  170.     }
  171.  
  172.     /**
  173.      * Registers and enqueues admin-specific styles.
  174.      *
  175.      * @since    1.0.0
  176.      */
  177.     public function register_admin_styles() {
  178.  
  179.         /*
  180.          * Check if the plugin has registered a settings page
  181.          * and if it has, make sure only to enqueue the scripts on the relevant screens
  182.          */
  183.  
  184.         if ( isset( $this->plugin_screen_slug ) ) {
  185.  
  186.             /*
  187.              * Check if current screen is the admin page for this plugin
  188.              * Don't enqueue stylesheet or JavaScript if it's not
  189.              */
  190.  
  191.             $screen = get_current_screen();
  192.             if ( $screen->id == $this->plugin_screen_slug ) {
  193.                 wp_enqueue_style( 'plugin-name-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), PLUGIN_NAME_VERSION );
  194.             }
  195.            
  196.         }
  197.        
  198.     }
  199.  
  200.     /**
  201.      * Registers and enqueues admin-specific JavaScript.
  202.      *
  203.      * @since    1.0.0
  204.      */
  205.     public function register_admin_scripts() {
  206.  
  207.         /*
  208.          * Check if the plugin has registered a settings page
  209.          * and if it has, make sure only to enqueue the scripts on the relevant screens
  210.          */
  211.  
  212.         if ( isset( $this->plugin_screen_slug ) ) {
  213.            
  214.             /*
  215.              * Check if current screen is the admin page for this plugin
  216.              * Don't enqueue stylesheet or JavaScript if it's not
  217.              */
  218.  
  219.             $screen = get_current_screen();
  220.             if ( $screen->id == $this->plugin_screen_slug ) {
  221.                 wp_enqueue_script( 'plugin-name-admin-script', plugins_url('js/admin.js', __FILE__), array( 'jquery' ), PLUGIN_NAME_VERSION );
  222.             }
  223.            
  224.         }
  225.        
  226.     }
  227.  
  228.     /**
  229.      * Registers and enqueues public-facing stylesheets.
  230.      *
  231.      * @since    1.0.0
  232.      */
  233.     public function register_plugin_styles() {
  234.         wp_enqueue_style( 'plugin-name-plugin-styles', plugins_url( 'css/display.css', __FILE__ ), PLUGIN_NAME_VERSION );
  235.     }
  236.  
  237.     /**
  238.      * Registers and enqueues public-facing JavaScript.
  239.      *
  240.      * @since    1.0.0
  241.      */
  242.     public function register_plugin_scripts() {
  243.         wp_enqueue_script( 'plugin-name-plugin-script', plugins_url( 'js/display.js', __FILE__ ), array( 'jquery' ), PLUGIN_NAME_VERSION );
  244.     }
  245.  
  246.     /**
  247.      * Registers the administration menu for this plugin into the WordPress Dashboard menu.
  248.      *
  249.      * @since    1.0.0
  250.      */
  251.     public function add_plugin_admin_menu() {
  252.        
  253.         /*
  254.          * TODO:
  255.          *
  256.          * Change 'Page Title' to the title of your plugin admin page
  257.          * Change 'Menu Text' to the text for menu item for the plugin settings page
  258.          * Change 'plugin-name' to the name of your plugin
  259.          */
  260.         $this->plugin_screen_slug = add_plugins_page(
  261.             __('Page Title', 'plugin-name-locale'),
  262.             __('Menu Text', 'plugin-name-locale'),
  263.             'read',
  264.             'plugin-name',
  265.             array( $this, 'display_plugin_admin_page' )
  266.         );
  267.        
  268.     }
  269.  
  270.     /**
  271.      * Renders the options page for this plugin.
  272.      *
  273.      * @since    1.0.0
  274.      */
  275.     public function display_plugin_admin_page() {
  276.         include_once('views/admin.php');
  277.     }
  278.  
  279.     /*
  280.      * NOTE:  Actions are points in the execution of a page or process
  281.      *        lifecycle that WordPress fires.
  282.      *
  283.      *        WordPress Actions: http://codex.wordpress.org/Plugin_API#Actions
  284.      *        Action Reference:  http://codex.wordpress.org/Plugin_API/Action_Reference
  285.      *
  286.      * @since    1.0.0
  287.      */
  288.     public function action_method_name() {
  289.         // TODO:    Define your action method here
  290.     }
  291.  
  292.     /*
  293.      * NOTE:  Filters are points of execution in which WordPress modifies data
  294.      *        before saving it or sending it to the browser.
  295.      *
  296.      *        WordPress Filters: http://codex.wordpress.org/Plugin_API#Filters
  297.      *        Filter Reference:  http://codex.wordpress.org/Plugin_API/Filter_Reference
  298.      *
  299.      * @since       1.0.0
  300.      */
  301.     public function filter_method_name() {
  302.         // TODO:    Define your filter method here
  303.     }
  304.  
  305. }
  306.  
  307. // TODO:    Update the instantiation call of your plugin to the name given at the class definition
  308. PluginName::get_instance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement