wordpress plugin boilerplate

Posted: November 20, 2014 in web

Structure:

plugin_dir
===css
======main.css
===js
======main.js

<?php
/*
  Plugin Name: Boiler
  Plugin URI:
  Description:
  Version: 0.1
  Author: em
  Author Email: me@home.com
  License:

  Copyright 2011  (me@home.com)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  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.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

class Boiler {
    
    const slug = 'boiler';
    static $add_script;

    function __construct() {

        add_action('init', array(__CLASS__, 'register_script'));
        add_action('wp_footer', array(__CLASS__, 'print_script'));
        add_action('init', array(&$this, 'init'));
    }

    function init() {
        $this->register_scripts_and_styles();
        add_shortcode('bioler', array(&$this, 'shortcode_boiler'));
    }

    function shortcode_boiler($atts) {
        extract(shortcode_atts(array(
            'name' => '',
            'icon' => '',
            'filter' => '',
            'default' => ''
                        ), $atts));
        $str = '<li class="tabs ' . $selected . '"><a class="' . $icon . '" data-filter="' . $filter . '">' . $name . '</a></li>';
        return $str;
    }

    static function register_script() {
        wp_register_script(self::slug . '-script-main', plugins_url('/js/main.js', __FILE__), array('jquery'), '1.0', true);
    }

    static function print_script() {
        if (!self::$add_script)
            return;
        wp_print_scripts(self::slug . '-script-main');
    }

    private function register_scripts_and_styles() {
        if (is_admin()) {
            
        } else {
            $this->load_file(self::slug . '-style-main', '/css/main.css');
        } 
    }

    private function load_file($name, $file_path, $is_script = false) {

        $url = plugins_url($file_path, __FILE__);
        $file = plugin_dir_path(__FILE__) . $file_path;

        if (file_exists($file)) {
            if ($is_script) {
                wp_register_script($name, $url, array('jquery')); //depends on jquery
                wp_enqueue_script($name);
            } else {
                wp_register_style($name, $url);
                wp_enqueue_style($name);
            }
        } 
    }

}

$boiler = new Boiler();

?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s