WordPress Plugin Development From Scratch Jonathan Desrosiers - - - PowerPoint PPT Presentation

wordpress plugin development from scratch
SMART_READER_LITE
LIVE PREVIEW

WordPress Plugin Development From Scratch Jonathan Desrosiers - - - PowerPoint PPT Presentation

WordPress Plugin Development From Scratch Jonathan Desrosiers - @Desrosj jonathandesrosiers.com About Me From: Dartmouth, MA Youre Here About Me Senior WordPress Engineer @ Linchpin (Pawtucket, RI) About Me Been using WordPress


slide-1
SLIDE 1

WordPress Plugin Development From Scratch

Jonathan Desrosiers - @Desrosj jonathandesrosiers.com

slide-2
SLIDE 2

About Me

  • From: Dartmouth, MA

You’re Here

slide-3
SLIDE 3

About Me

  • Senior WordPress Engineer @ Linchpin (Pawtucket, RI)
slide-4
SLIDE 4

About Me

  • Been using WordPress about 8 years
  • Theme Developer
  • Plugin Developer
  • Solution Developer
  • Developed 2 years on the WordPress VIP platform
slide-5
SLIDE 5

What’s a Plugin?

A WordPress Plugin is a program, or a set of one or more functions that adds a specific set of features, or alters the default behavior of a WordPress site. Plugins reside in the wp-content/plugins folder

slide-6
SLIDE 6

How Do They Work?

  • Action Hooks
  • Filter Hooks
  • WordPress APIs
slide-7
SLIDE 7

Actions

Actions are hooks that allow you to execute some code that does something. WordPress has a ton of these built in for you to use, but you can also create your own.

slide-8
SLIDE 8

Filters

Filters are hooks that allow you to CHANGE something. WordPress also has a ton of these built in for you to use, but you can also create your own.

slide-9
SLIDE 9

Goals Of This Workshop

  • Learn how to create a plugin from scratch.
  • Learn how to follow WordPress best practices
  • Learn how to effectively use hooks
  • Have a good time!
slide-10
SLIDE 10

Resources To Guide You

  • WordPress Codex: (http://codex.wordpress.org/)
  • WordPress PHP Coding Standards:


(https://make.wordpress.org/core/handbook/coding- standards/php/)

slide-11
SLIDE 11

Important Rules

  • Namespacing: Use a unique prefix to all of your

plugin’s functions

  • Avoids conflicts with WordPress core functions
  • Avoids conflicts with other plugins
slide-12
SLIDE 12

Namespacing

Bad Naming Conventions

slide-13
SLIDE 13

Namespacing

Good Naming Conventions

slide-14
SLIDE 14

WP_Debug Mode

  • Turn on WP_Debug mode in your wp-config.php file.
  • Will display errors to you on the screen for easier

debugging.

  • Help you write better code.
  • NEVER enable this on production.
slide-15
SLIDE 15

Never…

  • Develop a new plugin on a production site.
slide-16
SLIDE 16

Things Needed

  • A playground where you can develop.
  • Local environment
  • WordPress install on your hosting account
  • Anywhere you don’t care about something

breaking.

  • Code Editor
  • Slides & finished plugin file: jonathandesrosiers.com
slide-17
SLIDE 17

Questions?

  • Do you need help with setting up a local environment?
  • Do you need help with choosing a code editor?
  • Do you need help with your hair?
  • Familiarize yourself with action and filter hook names
  • http://codex.wordpress.org/Plugin_API/

Action_Reference

  • http://codex.wordpress.org/Plugin_API/Filter_Reference
slide-18
SLIDE 18

Creating Your Plugin

First part of every plugin is the File Header. This helps WordPress recognize the file as a plugin, and describes itself to admins so they know what they are activating.

slide-19
SLIDE 19

Adding to an Action Hook

slide-20
SLIDE 20

WordPress comes with some core post types (posts, pages, attachments), but sometimes we have other content that should be separate.

Create A Custom Post Type

Examples:

  • Movies
  • Locations
  • Products
  • Cars
slide-21
SLIDE 21

Create A Custom Post Type

  • Should be done within the ‘init’ action (initialization) hook.
slide-22
SLIDE 22

Create A Custom Post Type

  • Use the register_post_type() function
slide-23
SLIDE 23

Create A Custom Post Type

  • Use the register_post_type() function
slide-24
SLIDE 24

Create A Custom Post Type

slide-25
SLIDE 25
  • Also Should be done within the ‘init’ action hook.

Create A Custom Taxonomy

slide-26
SLIDE 26
  • Use register_taxonomy() function.

Create A Custom Taxonomy

slide-27
SLIDE 27

Create A Custom Taxonomy

slide-28
SLIDE 28
  • post_class() function outputs classes for a post.
  • Add to it with the ‘post_class’ filter.

Add CSS Class to Movie CPT

slide-29
SLIDE 29

Add CSS Class to Movie CPT

slide-30
SLIDE 30

Add CSS Class to Movie CPT

slide-31
SLIDE 31

Custom Post Meta

  • Metadata is data that describes other data
  • Use the WordPress Metadata API
slide-32
SLIDE 32

Custom Post Meta - Step 1

  • Register our post meta box
slide-33
SLIDE 33

Custom Post Meta - Step 1

  • Register our post meta box
slide-34
SLIDE 34
  • Display our post meta box.

Custom Post Meta - Step 2

slide-35
SLIDE 35
  • Display our post meta box.

Custom Post Meta - Step 2

slide-36
SLIDE 36

Custom Post Meta - Step 2

  • Display our post meta box.
slide-37
SLIDE 37
  • Save our post meta.
  • Use save_post action hook

Custom Post Meta - Step 2

slide-38
SLIDE 38
  • Save our post meta.

Custom Post Meta - Step 2

slide-39
SLIDE 39
  • Do something with the post meta.
  • Prepend it to our post content.
  • Append it to the title
  • Give admins the choice of where it should go

Custom Post Meta - Step 3

slide-40
SLIDE 40
  • Do something with the post meta - post content.

Custom Post Meta - Step 3

slide-41
SLIDE 41
  • Do something with the post meta - post title.

Custom Post Meta - Step 3

slide-42
SLIDE 42
  • Let’s add our custom setting
  • Use the ‘admin_init’ action hook.

Add Custom Setting

slide-43
SLIDE 43
  • Let’s add our custom setting

Add Custom Setting

slide-44
SLIDE 44
  • Let’s add our custom setting

Add Custom Setting

slide-45
SLIDE 45
  • Let’s add our custom setting

Add Custom Setting

slide-46
SLIDE 46
  • Use our new setting
  • Update our filters to only add one based on setting

Add Custom Setting

slide-47
SLIDE 47

Good Job!

  • Questions?
  • Twitter: @Desrosj
  • Slides & Finished Plugin: jonathandesrosiers.com