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.
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
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
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. Now let’s build our own
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
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
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
16. Step 2: Letuseradd taglineand save
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
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
19. Step 3: Give the admins aview
http://alphaparticle.com
AlphaParticle
keanan@alphaparticle.com
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
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
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. 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