How to use WP Autoloader for your own plugins?
-
You can use WP Autoloader to make development of WordPress plugins faster and easier.
WP Autoloader comes with an easy to use interface for plugins.
An example of plugin file:
<?php /* * Plugin Name: fancyBox * Plugin URI: https://bitbucket.org/holyfield/wp-fancybox * Description: <strong>fancyBox</strong> is a tool that offers a nice and elegant way to add zooming functionality for images, html content and multi-media on your webpages. It is built on the top of the popular JavaScript framework jQuery and is both easy to implement and a snap to customize. * Version: 2.1.4 * Author: Premium WordPress Apps * Author URI: https://wp-apps.co.uk/ * License: GNU General Public License */ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # FancyBox for WordPress # /** * Output message */ function plg_fancybox__admin_notice_wpa_missing() { $msg [ ] = '<div class="updated"><p>'; $msg [ ] = 'Plugin <strong>fancyBox</strong> is not hooked!<br>'; $msg [ ] = 'WordPress Autoloader plugin is required to use <strong>fancyBox</strong> plugin.<br>'; $msg [ ] = 'Please install <a href="https://bitbucket.org/holyfield/wp-autoloader" target="_blank">WordPress Autoloader</a> plugin first.'; $msg [ ] = '</p></div>'; echo implode( PHP_EOL, $msg ); } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Hook TubePlus Premium # if ( class_exists( '\WPAutoloader\AutoLoad' ) ) { \fancyBox\fancyBox::Hook(); } else { add_action( 'admin_notices', 'plg_fancybox__admin_notice_wpa_missing' ); }
As you see plugin file calls directly \fancyBox\fancyBox::Hook() without adding any include or request commands for related class files. All needed classes reside inside ./lib folder and a loaded automatically only when needed!
Lets check the file of related class itself. As you see our plugin class extends an abstract class. By using this abstract class you can save a lot of time. It’s not perfect yet, but still very robust and easy way to quickly develop plugins.
<?php namespace fancyBox; /** * * @version 2.1.4 */ final class fancyBox extends \WPAutoloader\Abstracts\Plugin { /** * Fancybox version * * @var string */ const version = '2.1.4'; /* (non-PHPdoc) * @see \WPAutoloader\Abstracts\Plugin::init() */ protected function init() { $plugin = self::getInstance(); # Add extra links on plugins page $plugin->_plugin_links['docs'] = '<a href="https://fancyapps.com/fancybox/#docs" target="_blank">' . __('Docs', 'fancyBox') . '</a>'; $plugin->_plugin_meta_row[] = '<a href="https://fancyapps.com/fancybox/" target="_blank">' . __('Visit fancyBox site', 'fancyBox') . '</a>'; # Define scripts and styles for front end # jquery-mousewheel self::AddJScript('jquery-mousewheel', self::GetUri('/inc/js/jquery.mousewheel-3.0.6.min.js'), 'jquery', '3.0.6'); # jquery-easing self::AddJScript('jquery-easing', self::GetUri('/inc/js/jquery.easing-1.3.min.js'), 'jquery', '1.3'); # fancybox self::AddJScript('fancybox', self::GetUri('/inc/js/jquery.fancybox.min.js'), 'jquery,jquery-mousewheel,jquery-easing', self::version); self::AddStyleSheet('fancybox', self::GetUri('/inc/css/jquery.fancybox.css'), '', self::version); # fancybox-thumbs self::AddJScript('fancybox-thumbs', self::GetUri('/inc/js/jquery.fancybox-thumbs.min.js'), 'jquery,fancybox', self::version); self::AddStyleSheet('fancybox-thumbs', self::GetUri('/inc/css/jquery.fancybox-thumbs.css'), 'fancybox', self::version); # fancybox-buttons self::AddJScript('fancybox-buttons', self::GetUri('/inc/js/jquery.fancybox-buttons.min.js'), 'jquery,fancybox', self::version); self::AddStyleSheet('fancybox-buttons', self::GetUri('/inc/css/jquery.fancybox-buttons.css'), 'fancybox', self::version); # fancybox-media self::AddJScript('fancybox-media', self::GetUri('/inc/js/jquery.fancybox-media.min.js'), 'jquery,fancybox', self::version); # fancybox-init self::AddJScript('fancybox-init', admin_url('admin-ajax.php') . '?action=fancyboxscript', 'jquery,fancybox', self::version); # Use front-end scripts on back-end $plugin->_admin_scripts = $plugin->_front_scripts; $plugin->_admin_styles = $plugin->_front_styles; self::AddAdminJScript('fancybox-settings', self::GetUri('/inc/js/jquery.wp.fancybox.min.js'), 'jquery,fancybox', self::version); # Load language add_action('plugins_loaded', 'fancyBox\fancyBox::LoadTextDomain'); } /** * Loads language */ public static function LoadTextDomain() { load_plugin_textdomain('fancyBox', false, self::GetBase('languages/')); } /** * * @since 2.1.4 */ public static function PrintFrontEndHeader() { } /** * * @since 2.1.4 */ public static function PrintAdminHeader() { } /** * * @since 2.1.4 */ public static function AddActionsAndFilters() { add_action('wp_ajax_nopriv_fancyboxscript', 'fancyBox\SettingsPanel::AjaxJSScript'); add_action('wp_ajax_fancyboxscript', 'fancyBox\SettingsPanel::AjaxJSScript'); add_action('admin_init', 'fancyBox\SettingsPanel::InitAdmin'); add_action('admin_menu', 'fancyBox\SettingsPanel::AdminMenu'); } }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to use WP Autoloader for your own plugins?’ is closed to new replies.