Hi, my name is Nate Allen!
WordPress Developer for over 10 years.
I specialize in backend/plugin development
Recently launched PeerRaiser!
About Me
@ncallen
github.com/nate-allen
About PeerRaiser
▪ WordPress plugin for nonprofit
organizations
▪ Peer-to-Peer fundraising
▪ Took over 2 years of development
▪ Caused me to learn MVC
Learn more…
https://PeerRaiser.com
Who is this talk for?
• Mid-level to Advanced Developers
• Understand some OOP concepts
• Have built a plugin, or want to
If you’re not an advanced developer, that’s okay too.
Let’s talk about Spaghetti Code!
What is spaghetti code?
▪ It’s all tangled together (tightly
coupled)
▪ It’s messy!
▪ Unlike spaghetti, it’s not delicious!
It’s hard to maintain.
Analogy: Creating a network
Connecting two computers… easy!
Analogy: Creating a network
Connecting three computers… looks good!
Analogy: Creating a network
Connecting six computers… getting complicated!
Analogy: Creating a network
Connecting ten computers… this is a mess!
Analogy: Creating a network
Is there a better way to connect many computers?
Analogy: Creating a network
Yes! Use a hub!
What is MVC?
• Architectural pattern invented in 1972 but
popularized by Ruby on Rails in 2005
• Organized and structured
• Separation of concerns
• Loose Coupling (unlike spaghetti!)
• Popular PHP frameworks include:
Laravel, Symfony, CodeIgniter, CakePHP
The Models
• The “Business Logic”
• The data, logic, info, and rules
• Get, Set, Calculate, etc.
• In WordPress plugins, they usually
represent database objects or custom
post types
The Views
• The “Presentation”
• What the user sees
• What the user interacts with
• Templates containing mostly HTML
and some PHP
The Controllers
• Intermediary between user and system
• Processes incoming requests and
determines which models and views are need
• Validation is done by the controller, like
checking if the user has permission to
complete the action
• Controller could also check if a form is filled
out correctly, and display an error (View)
or update the data (Model)
PHP + HTML = Bad
“Business Logic”
Business  Logic
Business  Logic
Just the “presentation”
(Isn’t this way easier?!)
Routers
• Most (if not all) MVC frameworks use a router to map the URL to a controller
• Example: https://domain.com/product/details/3
This would call the Product Controller, the details method, and pass the ID of 3
• In WordPress, URLs don’t map to routes, they map to queries
Arguments against MVC
• Not everyone agrees MVC is a good fit for
WordPress, and this is okay.
• WordPress is not an MVC framework!
It’s event driven.
• In WordPress, URLs don’t map to routes, they
map to queries
• It’s not the “WordPress Way”
• Can be overly complicated
Arguments for MVC
• Modern PHP frameworks use MVC:
Laravel, Symfony, CakePHP, Zend, CodeIgniter, Yii, etc.
• Models make backwards compatibility easier
• Views can be extended
• Separation of concerns
• There isn’t a “right way” to structure a plugin, as long as
you follow the coding standards.
Controllers without Routers
Actions, filters, and shortcodes go in the Controllers
Controller Example: Shortcode
Model Example
Two ways to use models the same model
General Specific
Model Constructor
The View Class
Getting markup from a
template file
Folder Structure
Example “To Do List” MVC Plugin:
https://github.com/nate-allen/to-do
Thank You!
Question? Comments?

Developing WordPress Plugins Using the MVC Methodology

  • 2.
    Hi, my nameis Nate Allen! WordPress Developer for over 10 years. I specialize in backend/plugin development Recently launched PeerRaiser! About Me @ncallen github.com/nate-allen
  • 3.
    About PeerRaiser ▪ WordPressplugin for nonprofit organizations ▪ Peer-to-Peer fundraising ▪ Took over 2 years of development ▪ Caused me to learn MVC Learn more… https://PeerRaiser.com
  • 4.
    Who is thistalk for? • Mid-level to Advanced Developers • Understand some OOP concepts • Have built a plugin, or want to If you’re not an advanced developer, that’s okay too.
  • 5.
    Let’s talk aboutSpaghetti Code! What is spaghetti code? ▪ It’s all tangled together (tightly coupled) ▪ It’s messy! ▪ Unlike spaghetti, it’s not delicious! It’s hard to maintain.
  • 6.
    Analogy: Creating anetwork Connecting two computers… easy!
  • 7.
    Analogy: Creating anetwork Connecting three computers… looks good!
  • 8.
    Analogy: Creating anetwork Connecting six computers… getting complicated!
  • 9.
    Analogy: Creating anetwork Connecting ten computers… this is a mess!
  • 10.
    Analogy: Creating anetwork Is there a better way to connect many computers?
  • 11.
    Analogy: Creating anetwork Yes! Use a hub!
  • 12.
    What is MVC? •Architectural pattern invented in 1972 but popularized by Ruby on Rails in 2005 • Organized and structured • Separation of concerns • Loose Coupling (unlike spaghetti!) • Popular PHP frameworks include: Laravel, Symfony, CodeIgniter, CakePHP
  • 13.
    The Models • The“Business Logic” • The data, logic, info, and rules • Get, Set, Calculate, etc. • In WordPress plugins, they usually represent database objects or custom post types
  • 14.
    The Views • The“Presentation” • What the user sees • What the user interacts with • Templates containing mostly HTML and some PHP
  • 15.
    The Controllers • Intermediarybetween user and system • Processes incoming requests and determines which models and views are need • Validation is done by the controller, like checking if the user has permission to complete the action • Controller could also check if a form is filled out correctly, and display an error (View) or update the data (Model)
  • 16.
  • 17.
  • 18.
  • 19.
    Routers • Most (ifnot all) MVC frameworks use a router to map the URL to a controller • Example: https://domain.com/product/details/3 This would call the Product Controller, the details method, and pass the ID of 3 • In WordPress, URLs don’t map to routes, they map to queries
  • 20.
    Arguments against MVC •Not everyone agrees MVC is a good fit for WordPress, and this is okay. • WordPress is not an MVC framework! It’s event driven. • In WordPress, URLs don’t map to routes, they map to queries • It’s not the “WordPress Way” • Can be overly complicated
  • 21.
    Arguments for MVC •Modern PHP frameworks use MVC: Laravel, Symfony, CakePHP, Zend, CodeIgniter, Yii, etc. • Models make backwards compatibility easier • Views can be extended • Separation of concerns • There isn’t a “right way” to structure a plugin, as long as you follow the coding standards.
  • 22.
    Controllers without Routers Actions,filters, and shortcodes go in the Controllers
  • 23.
  • 24.
    Model Example Two waysto use models the same model General Specific
  • 25.
  • 26.
    The View Class Gettingmarkup from a template file
  • 27.
  • 28.
    Example “To DoList” MVC Plugin: https://github.com/nate-allen/to-do
  • 29.

Editor's Notes

  • #4 Kickstarter for nonprofits
  • #5 1 connection
  • #7 1 connection
  • #8 3 connections
  • #9 15 connections
  • #10 45 connections
  • #11 16 computers… 120 connections if connected directly. 1000 computers would require 499,500 connections!
  • #13 Yes, it’s old.
  • #15 This is the simplest one to understand
  • #17 Enough theory, let’s look at some code A lot of PHP and HTML being mixed together Have to open and close PHP tags a lot. Hard to indent properly Security issue: The output isn’t being escaped (OCD)
  • #18 Highlighted: Business logic. What happens if “staff” isn’t a custom post type anymore? What if meta field names change? What if the logic for full name should change?
  • #19 This code would be in its own file in the Views folder. $staff_members is an arrow of $staff_member objects that the controller gets from the staff model
  • #20 Want to quickly talk about routers because almost all MVC frameworks use them
  • #21 WordPress is not an MVC framework! (show event flow chart) This is true. But there are ways of structuring your plugin to be MVC It’s not the “WordPress way” Trying to shoehorn MVC into WordPress doesn’t feel right. I agree that trying to force typical MVC routing doesn’t work well I’ll show you how to MVC without the typical router It can be overly complicated Small plugin that does 1 thing? Don’t need MVC. This is true. But keep in mind that your small plugin may grow some day…
  • #22 Knowing MVC can make you a better developer. You may need to work in Laravel in the future. Custom post types vs custom database tables. Post meta vs taxonomies, etc. My plugin looks for template files in the theme first, making it easier to people to modify my plugin My opinion, but I think themes have a pretty set agreed upon structure.
  • #23 WordPress is event driven. Instead of Controllers responding to just user actions, we can respond to WordPress actions I don’t put these in a __construct function
  • #24 WordPress is event driven. Instead of Controllers responding to just user actions, we can respond to WordPress actions I don’t put these in a __construct function
  • #25 When I create a model, I design it in a way so we can use it for general or specific purposes
  • #26 The construct method is called automatically when an object is being created
  • #27 When I create a model, I design it in a way so we can use it for general or specific purposes