17
Hi, my name is Topher I’m a WordPress developer from Grand Rapids MI @topher1kenobe Intro To Plugin Development Topher DeRosia @topher1kenobe

Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

Embed Size (px)

Citation preview

Page 1: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

Hi, my name is TopherI’m a WordPress developer from

Grand Rapids MI

@topher1kenobe

Intro To Plugin Development

Topher DeRosia@topher1kenobe

Page 2: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

Intro To Plugin Development

Emptying out functions.php

Intro To Plugin DevelopmentTopher DeRosia

@topher1kenobe

Page 3: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

“Just put this code in your theme’s functions.php file…”

JUST SAY NO

Intro To Plugin Development

Topher DeRosia@topher1kenobe

Page 4: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 5: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

Plugins are either single files or folders in /wp-content/plugins

Intro To Plugin Development

Topher DeRosia@topher1kenobe

Page 6: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 7: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

In the top of every main plugin file is a header block with information about the plugin.

Intro To Plugin Development

Topher DeRosia@topher1kenobe

Page 8: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 9: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 10: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 11: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 12: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 13: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 14: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

mu-plugins

mu == must use

Plugins 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

Page 15: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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: Simply 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

Page 16: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

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

Page 17: Introduction to WordPress Plugin Development, WordCamp North Canton, 2015

THANKS FOR

LISTENING

Intro To Plugin DevelopmentTopher DeRosia

@topher1kenobe

http://topher1kenobe.comFollow me @topher1kenobe