Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Buildingyour First
WordPress Plugin
Keanan Koppenhaver
CTO, Alpha Particle
http://alphaparticle.com
AlphaParticle
keanan@a...
Importantinfo
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Code: https://github.com/kkoppenhaver/wp-aut...
Whatisaplugin?
• Piece of PHP code that can be enabled or disabled through
wp-admin and adds functionality to WordPress
• ...
Let’s Lookatasimple plugin
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Hello Dolly
• https://wordpress.org/plugins/hello-dolly
• One of the original plugins written by Matt Mullenweg
• When act...
Demo: Hello Dolly
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Hooksand filters
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Whatare Hooksand Filters?
• add_action( 'admin_notices', 'hello_dolly' );
• Hooks and filters provide the ability to chang...
Now let’s build our own
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Basic requirements
• We want to build a plugin that lets authors pick a tagline and lets
them display that tagline using a...
Step 0: Initialplugin setup
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
DEMO
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Step 1: Custom Database Table
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Custom Database Tables
• Can be added when data that doesn’t match an existing
WordPress data format needs to be stored
• ...
DEMO
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Step 2: Letuseradd taglineand save
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Custom User Fields
• We can use WordPress hooks to add an input field to the User
edit screen that an author can use to up...
DEMO
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Step 3: Give the admins aview
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Admins should beable to see
everything
• Let’s give them a table where they can view all their author’s
taglines
• We’ll c...
DEMO
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Step 4: OutputTagline
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Letauthors displaytheir taglines
• We’ll create a shortcode so that the tagline can be inserted
into any post
• Whenever o...
DEMO
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Distribution
• Submit to the WordPress plugin repo (will need a README file
and comply with coding/security standards)
• D...
GeneralTipsand tricks
• Namespace your functions (either in a class or by using a prefix)
• All our function names were pr...
Questions?
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
Code: https://github.com/kkoppenhaver/wp-author...
Upcoming SlideShare
Loading in …5
×

Building a WordPress plugin

244 views

Published on

Ever wanted to build your own WordPress plugin from scratch? Here's your chance. We'll look at what a WordPress plugin is, how a simple plugin is constructed, and then we'll jump right into building our own. Bring your laptop and code with us or follow along as we go and learn some of the tools WordPress already has in place to allow you to extend core functionality and get your plugin in the repository.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Building a WordPress plugin

  1. 1. Buildingyour First WordPress Plugin Keanan Koppenhaver CTO, Alpha Particle http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  2. 2. Importantinfo http://alphaparticle.com AlphaParticle keanan@alphaparticle.com Code: https://github.com/kkoppenhaver/wp-author-tagline Slides: https://goo.gl/wZVMmo
  3. 3. Whatisaplugin? • Piece of PHP code that can be enabled or disabled through wp-admin and adds functionality to WordPress • Can be distributed through the WordPress Plugin Respository (https://wordpress.org/plugins) • Over 50,000 plugins as of 08/2017 http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  4. 4. Let’s Lookatasimple plugin http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  5. 5. Hello Dolly • https://wordpress.org/plugins/hello-dolly • One of the original plugins written by Matt Mullenweg • When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page. http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  6. 6. Demo: Hello Dolly http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  7. 7. Hooksand filters http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  8. 8. Whatare Hooksand Filters? • add_action( 'admin_notices', 'hello_dolly' ); • Hooks and filters provide the ability to change and modify WordPress without modifying the core • Triggered at certain times based on what WordPress is loading and when http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  9. 9. Now let’s build our own http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  10. 10. Basic requirements • We want to build a plugin that lets authors pick a tagline and lets them display that tagline using a shortcode • Main Features • Create a custom DB table for storing these values and associating them with authors • Let authors modify their own tagline directly on their User page • Create a settings page so that admins can see all of the currently set tag lines • Let the author display their tagline anywhere they want using a shortcode http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  11. 11. Step 0: Initialplugin setup http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  12. 12. DEMO http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  13. 13. Step 1: Custom Database Table http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  14. 14. Custom Database Tables • Can be added when data that doesn’t match an existing WordPress data format needs to be stored • In our case, we’ll create a simple table that stores author ID and author tagline • More simply, we could be storing this as metadata on the User object, but we want to see how to create custom DB tables • Two tasks: Create our DB table when the plugin is activated and clean it up when the plugin is deleted (not deactivated) http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  15. 15. DEMO http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  16. 16. Step 2: Letuseradd taglineand save http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  17. 17. Custom User Fields • We can use WordPress hooks to add an input field to the User edit screen that an author can use to update their tagline • We’ll save this in our custom database table • Validation of this input is important. NEVER TRUST THE USER (even when they’re logged into wp-admin) http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  18. 18. DEMO http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  19. 19. Step 3: Give the admins aview http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  20. 20. Admins should beable to see everything • Let’s give them a table where they can view all their author’s taglines • We’ll create a plugin settings page that only admins can access underneath the Tools menu in wp-admin • For a fancier version of this we could use the WP_List_Table class, but look very closely on that implementation http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  21. 21. DEMO http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  22. 22. Step 4: OutputTagline http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  23. 23. Letauthors displaytheir taglines • We’ll create a shortcode so that the tagline can be inserted into any post • Whenever our plugin sees [tagline] in a post, it will query the DB and echo out the post author’s tagline http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  24. 24. DEMO http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  25. 25. Distribution • Submit to the WordPress plugin repo (will need a README file and comply with coding/security standards) • Distribute through GitHub or your own site • Sell through Envato or other similar marketplaces http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  26. 26. GeneralTipsand tricks • Namespace your functions (either in a class or by using a prefix) • All our function names were prefixed by wpat_ so that our function names didn’t conflict with anything in WordPress or other themes/plugins • Be sensible with what you enqueue (don't require a bunch of CSS/JS with your plugin that’ll slow down your users site) • Test your plugins and keep them up-to-date with WordPress releases • Use WordPress coding standards to help with submitting to the plugin repo http://alphaparticle.com AlphaParticle keanan@alphaparticle.com
  27. 27. Questions? http://alphaparticle.com AlphaParticle keanan@alphaparticle.com Code: https://github.com/kkoppenhaver/wp-author-tagline Slides: https://goo.gl/wZVMmo

×