The document is a transcript from a presentation by Chad Windnagle on how to be a good developer citizen when working with WordPress. Some of the key points discussed include using object-oriented programming when building WordPress plugins, avoiding functionality in themes and instead using plugins, following coding best practices like consistent naming conventions and limiting indentation levels, and using tools like PHP Code Sniffer to check for code style compliance. The presenter advocates for building reusable code with modern PHP techniques to make development faster and easier.
22. WordCamp Orlando, 2015 - Chad Windnagle
Themes should never
• Touch $wp_query
• Change the post content
• Change the post title
• Change the meta data
• Change URL parameter
• Change anything except CSS, javascript, and markup
23. WordCamp Orlando, 2015 - Chad Windnagle
If your site will not function
the same with a different
theme, you are doing it
wrong.
37. WordCamp Orlando, 2015 - Chad Windnagle
Most information I
found:
• Not object oriented
• Bad function names
• Required Vendor prefixed
• Inconsistent Code Style
49. WordCamp Orlando, 2015 - Chad Windnagle
Major PHP Wins:
• Object oriented code
• Reusable Code
• Entering into SOLID programming
• DRY Methods.
• Code that can be extended
• Code that can be inherited
• Flexible Coding FTW
50. WordCamp Orlando, 2015 - Chad Windnagle
Why have class?
• Clean fast reusable code
• Saves time & money
• Happy developers & Happy users
86. WordCamp Orlando, 2015 - Chad Windnagle
This will force you to:
create more methods (DRY! don’t repeat
yourself)
throw more exceptions
do more error checking
think about code-scenarios less
Hi Everyone
Thanks so much for coming. I know there are a lot of other great sessions on the schedule for this slot you could have gone too, so I really appreciate you taking the time to come to this one. I hope when you leave you feel like it was time well spent on your part!
I want to give you a quick introduction about myself so you understand a bit where I’m coming from and why I have some of the opinions I do.
Also you can consider this a bit of a disclaimer if you don’t like anything in my talk - I have a good excuse!
I am not a wordpress guy. In fact I’ve only been working on wordpress sites for about 2 months in a full time, regular capacity.
I’m really a joomla, symfony, lavaravel, and general all around php developer.
I spent the last 8+ years doing consulting work specializing in Joomla and writing a lot of php.
These days I work a lot on with Laravel, and a bit with wordpress.
So, my experience has lead me to appreciate a few finer things in life:
Symfony and Laravel, decent php frameworks for building custom applications.
I love good code, and I realize good is an opinion or interpretation. We’ll get to defining that shortly.
I *love* object oriented programming. Once you get away from procedural style coding and learn the power and simplicity of OOP, you’ll fall in love. I know I did!
I love good documentation. This is an area that wordpress really excells. There’s tons of great documentation everywhere.
I love to test code. Unit testing, code style testing, system tests. I want to be completely sure that my code works the way it is supposed too.
These thigns, php frameworks, good quality code, object oriented style programming - these are a few of my most favorite things when it comes to working on code.
They really have to be, because when you jump around from project to project, it’s extremely important to be able to quickly pick up the new set of code, and start being productive, or fixing bugs, or whatever else you have to do, right away. Don’t waste time learning having to learn crazy code.
So with all those things that I love, and with all the different tools I’ve worked with - why in the world am I at a wordpress conference?
I want to bring modern, object oriented style programming to the WordPress community. I want you to see the light! There are some great techniques of modern php practices that will save you time, money, effort, and a few brain cells.
Let’s get started.
So what does all of that back story mean? do I think I’m some sort of big deal?
Do I think you should trust me because I’m a time lord and I’m smarter than all of you?
No! Definitely not!
I’m a newbie to wordpress. A lot of this is just best practices established by php community experts.
So with that!
Bro - trust me - it will be awesome!!
Okay - Here we go. And I know, I already know, this first topic might be a little controversial to you! I pitched it to a few of my wordpress friends the other day and it took a while for it all to sink in.
just relax, hold your breath and try to understand where I’m coming from!
We need to stop using themes for functionality.
Stop it! that’s not what themes are intended to be doing!
Themes are for presentation - they are for styling, they are for javascript enhancements.
Now you might say to me “but chad I need functions.php! how else will I do ___”
You really don’t. I promise you don’t. and if you do, you might need to ask yourself if you should be doing what you’re trying to do!
You really just need to make a plugin! Plugins are almost always the right answer.
In this example we have a plugin that is written around the class-based design.
we have a constructor
executed when we instantiate
we have the $this variable which references the instance of the class.
This is an application class.
Vendor prefix
- we can easily change the prefix
- renaming because of a collision is much much easier
- function names are better.
An abstract class could be like a telephone.
The developer knows that the most basic features will be to send and receive phone calls.
But a more advanced model might be things like voicemail, texting, call waiting, etc.. Rather than rewriting the send and receive calls, we can inherit those features, and add new ones without rewriting the same old features.
This is object oriented programming.
SOLID = Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion.
DRY = do not repeat yourself
This is object oriented programming.
This is object oriented programming.
A nonce is a number. used once.
In wordpress its actually used more than once, but that’s another topic.
The nonce helps wordpress determine if this is a legit form submission. it prevents users from double-entry, protects you from spam, etc… basically you should have this on all your forms.
Any clues?
change this example to get user and ! user exists throw exception.
Types of exceptions before error logs
Different types of severity
different types of action (log error, throw to user, display, send email notification, etc..)
method and variable names that make sense
I’ve literally gone on to google and googled synonmons and adjectives and verbs to help me think of good variable and method names.