Post on 07-Aug-2015
transcript
Intro To Plugin Development
Emptying out functions.php
Intro To Plugin DevelopmentTopher DeRosia
@topher1kenobe
Hi, my name is TopherI’m a WordPress developer from
Grand Rapids MI
@topher1kenobe
Intro To Plugin Development
Topher DeRosia@topher1kenobe
“Just put this code in your theme’s functions.php file…”
JUST SAY NO
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Plugins are packages of code that affect your site’s functionality.
Themes are packages of code that affect your site’s design.
DO NOT MIX.Intro To Plugin Development
Topher DeRosia@topher1kenobe
Plugins are either single files or folders in /wp-content/plugins
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Typically inside each plugin folder is a file with the same name as the folder, plus any other files it needs.
Intro To Plugin Development
Topher DeRosia@topher1kenobe
In the top of every main plugin file is a header block with information about the plugin.
Intro To Plugin Development
Topher DeRosia@topher1kenobe
The only option absolutely required is the “Plugin Name”. The others are merely strongly recommended.
VERY strongly recommended:License: GNU General Public License v2 or laterLicense URI: http://www.gnu.org/licenses/gpl-2.0.htmlTags: custom-header, custom-menu, editor-style, featured-images (etc)
Intro To Plugin Development
Topher DeRosia@topher1kenobe
The Secret Sauce:
Any code you might have put into functions.php in your theme could go into a plugin.*
*with a few exceptions
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Example:
<?php/*Plugin Name: Topher’s Little PluginDescription: Does things I want to my siteVersion: 1.0Author: Topher*/
// 220 pixels wide by 180 pixels tall, hard crop modeadd_image_size( 'custom-size', 220, 180, true );?> Intro To Plugin Development
Topher DeRosia@topher1kenobe
Longer Example:<?php/*Plugin Name: Topher’s Little PluginDescription: Does things I want to my siteVersion: 1.0Author: Topher*/
// 220 pixels wide by 180 pixels tall, hard crop modeadd_image_size( 'custom-size', 220, 180, true );
// Make shortcodes work inside text widgetsadd_filter('widget_text', 'do_shortcode');
// Fix styling in old contentfunction phlog_scripts() {
wp_enqueue_style( 'phlog_styles', plugins_url( '/css/phlog.css' , __FILE__ ) );}add_action( 'wp_enqueue_scripts', 'phlog_scripts' );
?> Intro To Plugin Development
Topher DeRosia@topher1kenobe
Explanation:
function phlog_scripts() {wp_enqueue_style( 'phlog_styles', plugins_url( '/css/phlog.css' , __FILE__ ) );
}add_action( 'wp_enqueue_scripts', 'phlog_scripts' );
I made a function called `phlog_scripts()`
Inside it is a function that properly enqueues a CSS file. wp_enqueue_style takes 2 arguments, a name I made up and the path to the CSS file.
The path to the CSS file is determined with the WordPress function plugins_url().
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Releasing a Plugin
A plugin built for release on WordPress.org must meet a list of requirements. The requirements are listed at
https://developer.wordpress.org/plugins/wordpress-org/
Best practices are found in the WordPress Plugin Handbook
https://developer.wordpress.org/plugins/
Intro To Plugin Development
Topher DeRosia@topher1kenobe
mu-plugins: mu == must usePlugins that are stored in mu-plugins are automatically activated, and cannot be deactivated. The main file of a plugin must be stored directly in the /mu-plugins/ directory, OR be included.
Intro To Plugin Development
Topher DeRosia@topher1kenobe
mu-plugins
Intro To Plugin Development
Topher DeRosia@topher1kenobe
mu-plugins
To convert a regular plugin to mu-plugins, put it in the /mu-plugins directory and then make a new, single-file plugin that looks like this:
<?php/*Plugin Name: mu-plugins inclusion pluginDescription: Includes the main files of any plugins that are in mu-pluginsAuthor: TopherVersion: 1.0*/
// include widget_logicinclude WP_CONTENT_DIR . '/mu-plugins/widget-logic/widget_logic.php';
?>
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Extra Credit: WP-CLI
WP-CLI is the command line tool for WordPress. It can create an empty plugin with the proper header in place:
wp scaffold plugin --prompt
This will ask questions like this:
1/4 <slug>: topher-test2/4 [--plugin_name=<title>]: Topher's Test3/4 [--skip-tests] (Y/n): Y4/4 [--activate] (Y/n): YSuccess: Created /home/topher1/topher1kenobe.com/wp-content/plugins/topher-test1/1 <plugin>: topher-testSuccess: Created test files.
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Real World ExampleEasy Digital Downloads - Customer Contact
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Easy Digital Downloads - Customer Contact
<?php/*Plugin Name: Easy Digital Downloads - Customer ContactVersion: 1.0Description: Creates a tab on the Customer page for sending an email to that customer.Author: TopherAuthor URI: http://topher1kenobe.comPlugin URI: http://topher1kenobe.comText Domain: edd-customer-contactDomain Path: /languagesLicense: GNU General Public License v2 or laterLicense URI: http://www.gnu.org/licenses/gpl-2.0.html*/
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Easy Digital Downloads - Customer Contact
Comment everything!
Docblock/** * Contact a customer * * @since 2.3 * @param array $args The $_POST array being passeed * @return int Whether it was a successful email */
Inline// make sure we're allowed to be here at allif ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) { wp_die( __( 'You do not have permission to contact this customer.', 'edd' ) );} Intro To Plugin Development
Topher DeRosia@topher1kenobe
Easy Digital Downloads - Customer Contact
Insert Code Review Here
Intro To Plugin Development
Topher DeRosia@topher1kenobe
Easy Digital Downloads - Customer Contact
Get Help
From other developers (Slack, Stack Overflow, friends)
From parent plugin developer (if making an Addon they probably want to help)
From documentation (https://developer.wordpress.org/plugins/)
From code (read through plugins doing similar things)
Intro To Plugin Development
Topher DeRosia@topher1kenobe
THANKS FOR
LISTENING
Intro To Plugin DevelopmentTopher DeRosia
@topher1kenobe
http://topher1kenobe.comFollow me @topher1kenobe