GOT AWESOMETHEMEFUNCTIONALITY?!IT PROBABLYNEEDS A PLUGINCore Functionality Plugin + TGM Plugin Activation + WP_FilesystemBy: Thomas Grifﬁn / @jthomasgrifﬁn / thomasgrifﬁnmedia.com
A LITTLE BIT!ABOUT ME I DEVELOP WORDPRESS FOR A LIVING I hold a degree in Business Management and Administration from the best college in the world – Appalachian State University. I ofﬁcially started September 2010, but have been at this whole developing thing for around two years. Fun fact: I am a scratch golfer.
THE DOWN!AND DIRTY WHERE ARE WE GOING TODAY? 1. Core Functionality plugin 1. Anatomy of a plugin 2. What is a core functionality plugin and why is it important? 2. TGM Plugin Activation class 1. Leveraging plugins for your users 2. Requiring/recommending plugins with your themes 3. Practical examples 3. WP_Filesystem (if we have time) 1. Brief tutorial and how-to’s 4. Q and A
PLUGINS, PLUGINS,! PROTECT YOURSELFPLUGINS ! A core functionality plugin contains (what should be) themeCORE independent elements of a site.FUNCTIONALITY PLUGINS This can be a range of things, but generally this includes shortcodes,PROTECT metaboxes and CPT’s/CT’s.YOU FROM Why? Users expect these things toANGRY, SNARKY, UPSET, RUDE, INSENSITIVE remain when switching themes. AtIRATE, IRRITATED, ANNOYED & PEEVED worst, this saves you hours of support calls.USERS AND CLIENTS Themes shouldn’t be the end-all- be-all for your projects. Ever.
BUILDING A PLUGIN (CONT)A COUPLE OF USEFUL FUNCTIONS• plugin_dir_path( __FILE__ ) • __FILE__ is a PHP constant that gets the full path and ﬁlename of the current ﬁle• plugins_url( $path, $plugin ) • $path is the path to whatever you are calling (e.g. an image or script) • $plugin is the path to the plugin (we can use __FILE__ here)• Both are documented in the Codex – look them up
BUT WAIT I KNOW YOU ARE THINKING… How can I include plugins within a theme? How will users know how to install them? How can I give users a way to install them? What about private plugins? Can I pull plugins from WordPress repo? What about pre-packaged plugins? ENTER TGM PLUGIN ACTIVATION
TGM PLUGIN!ACTIVATION TGM PLUGIN ACTIVATION is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference pre- packaged plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet. http://tgmpluginactivation.com
WHY !USE IT? SIMPLE: IT IS SILLY NOT TO The library uses all of WordPress’ own classes and methods in the installation process. It even works for those users who have strict permissions. And it works for MultiSite, too.
WHY !USE IT? (CONT) The are a number of beneﬁts to using a plugin for your functionality vs. including it directly in the theme: 1. To reiterate, it saves you from having to deal with “urgent” messes later 2. Easy updating and management (WP repo or even your own private repo) 3. If the project allows, you can share your code with others 4. Users can’t mess their own site up (no WSOD if they edit it incorrectly) Let’s dive into the library, shall we?
WP_FILESYSTEMWP_Filesystem is a function used to call various WP_Filesystem_*classes depending on a user’s server setup.There are 4 different “methods” that WP_Filesystem uses:1. Direct2. FTP3. FTP Sockets4. SSH2WP_Filesystem will automatically choose the best method basedon the server setup.
WHY WP_FILESYSTEM?Writing local ﬁles, if not done correctly (err, not done withWP_Filesystem), poses security risks.Why? It is highly unlikely that your webserver user and your useraccount are one in the same. Consider shared hosting – it has awebserver user and lots of other users (like you on it). Give thewebserver user ownership of your ﬁles and now everyone hasaccess to them – no bueno.In effect, WP_Filesystem makes sure that you in fact are the onemaking changes to your ﬁles.
USING WP_FILESYSTEM?Due to a lack of time, we’ll dive into the TGM Plugin Activationlibrary to see how WP_Filesystem is setup to process plugininstallations.There is one important function you need to know, though:request_ﬁlesystem_credentials( $form_post, $type, $error = false,$context, $extra_ﬁelds )And once setup, you can call the WP_Filesystem object:global $wp_ﬁlesystem
WRAPPING UP1. Use a core functionality plugin when working on projects2. Don’t include functionality in a theme that should be in a plugin3. Require/recommend plugins within your distributed/ commercial themes with the TGM Plugin Activation library4. Use WP_Filesystem to do any local ﬁle writing within your plugins or themes