Category Archives: Programming

Polymorphism and WordPress: Abstract classes

When we covered inheritance, there were some questions about interfaces and abstract classes.

  • What can you use them for?
  • What are the advantages?
  • When should you use them?

These are all great questions that are worth exploring. As the title suggests, “Polymorphism” is the object-oriented feature that helps answer these questions. It’s not an easy feature to grasp.

That’s why most of the article will be about an in-depth example. You’ll see the thought process involved with using it. This will help you understand it better so you can apply it to your own projects.

Continue reading Polymorphism and WordPress: Abstract classes

Using inheritance with WordPress

As a WordPress developer, you’re always looking for ways to better reuse your code between projects. Your time is valuable and you don’t want to reinvent the wheel each time you start a new project.

Object-oriented programming can help you with that. In a previous post, I covered why you should learn it. Now it’s time to take things further by going over the main feature for code reuse. You’ve probably heard about it before. It’s called “inheritance“.

Continue reading Using inheritance with WordPress

The first thing you should learn from object-oriented programming

You’ve decided to learn object-oriented programming, but you don’t know where to start. Object-oriented programming has so many concepts and features. The whole thing can feel overwhelming at times. Let’s help you get started on the right foot.

What’s a great place to start? With the feature that you’ll associate the most with object-oriented programming. It’s called encapsulation. Most modern programming languages support encapsulation using classes.

Continue reading The first thing you should learn from object-oriented programming

Why object-oriented programming is your next step as a WordPress developer

You’re a WordPress developer. You might have a few plugins under your belt or a theme or two. You build WordPress sites for clients or just for yourself. You’ve heard of object-oriented programming, but, each time you look into it, it makes no sense!

You tell yourself object-oriented programming isn’t useful or worth the trouble. The goal of this article is to focus on explaining this value to you. At the end of this article, you should have a clear understanding of why you should learn it.

Continue reading Why object-oriented programming is your next step as a WordPress developer

Single responsibility principle, WordPress and You

For WordPress developers, it can be hard to improve your PHP skills. You lack the resources or tutorials to drive home these concepts. This happened just a few weeks ago when Nathaniel asked for help with his Stack Overflow question.

He was looking for help applying the single responsibility principle with WordPress. I sent him a bunch of replies but closed off saying I needed to write a post about it. This article is a detailed explanation of my thoughts following that conversation.

As WordPress developer, you might have started working with or looking into object-oriented programming. Maybe you even heard of something called SOLID.

As a whole, SOLID can be an intimidating topic. But the single responsibility principle is just one part of it (it’s the ‘S’ in SOLID). That makes it a more manageable topic to discuss and help you with.

At the end of this article, you should have a better understanding of it. You can also use the provided code samples in your own plugins and themes.

Continue reading Single responsibility principle, WordPress and You

A plugin for a rainy day

Moving ahead from our last post, I want to present the beta version of the Helthe WordPress Error Monitoring plugin (available here). The overall goal of the plugin is to offer relevant error monitoring for your production WordPress site.

A debugging tale

When debugging WordPress (or any other programming problem for that matter), what do you usually do? You start using your badass Google-fu to find a solution and, when that fails, you post on support forums, WordPress Answers (Stack Overflow) or another medium, but I am getting ahead of myself.

Before that you need to know what to look for and that’s usually where the problem lies especially with WordPress. Unless you’re dealing with a fatal error (White Screen of Death), WordPress is generally pretty silent about issues. This is by design.

Great experience for the users. Hell for supporting them.

This is plugin is a serious attempt at helping you with that. I have already done a good effort to capture a lot of the known errors (and some new ones as well), but having you use it will help me find additional ways to help you (and find bugs) when you need it the most.

What are you getting?

I don’t plan on doing a feature by feature explanation, but I wanted to highlight a few things that I hope you will find useful.

Leveraging existing plugins

The first thing I did is consolidate some of the good stuff from the Core Control plugin and Deprecated Log Notices which do not seem to be actively worked on anymore. Those were the two plugins I knew that helped for specific problems (let me know if you know others!).

With this, you’ll know if you’re using deprecated functions or calling functions wrong. However, the more interesting element is tracking errors from the HTTP API. Without diving to deep into it, the HTTP API is used by WordPress to handle communications with other web services. This includes wp-cron and errors with the API can be one of the reasons why your cron tasks (like schedule posts) do not run.

Termination Errors

This has the potential to be very useful. Have you ever had AJAX errors with WordPress? Those can be really annoying to solve since you can’t see the output. Well now, anytime WordPress terminates with an error, it will get logged in your error log. I haven’t seen anything like this before so I am hoping that this turns out to be very useful for you.

Image Editor Errors

Another common problem you can encounter is the image editor not working because you’re missing libraries for example. This is a scenario where you really won’t know unless you spend quite a bit of time poking around.

Admin Bar Support

Wondering if there were errors when your page rendered, you’ll be able to see all the errors that were logged by the plugin in the admin bar. This is just for site administrators.

And more!

There’s quite a few more things already under the hood and, as far as I know, there’s nothing another error plugin does that isn’t covered with this current beta version. I am not satisfied yet though. The same way you think of Akismet as your default spam solution, I  want this to be a must-have for any serious WordPress professional.

I can’t do that without your help and feedback. Try it out and create issues whenever you have a production (or even development) problem that wasn’t caught by the plugin and caused you serious pain.

This is available to you for free

You won’t need to sign up for anything and you will get all the errors logged in the PHP error log. I think for this to be truly great for the community, all the features need to be available to everyone.

Where do Helthe fit in this?

Right now the plugin only writes to the PHP error log. When Helthe gets closer to the beta phase, there will be an option to add an API key that will let errors be sent to the service. The purpose of Helthe is to make error monitoring and management more convenient for you so if you’re ok with looking through your error logs then you won’t need anything else to be functional.

As always, feel free to leave a comment below or discuss on Hacker News.

P.S. If you’d like to keep up to date with my progress with Helthe, leave your email below!

[mailchimpsf_form]

Diving into WordPress errors

I would say that the most uncertain aspect of Helthe has been if it could do an amazing job monitoring WordPress errors. In the past, finding out what a problem was in WordPress has been a very frustrating (even table-flipping) experience for myself and a lot of people I have asked about it. So I set out to test and work on this critical assumption.

Can we have good error monitoring in WordPress? Is it already available? Here are my findings after 2 weeks.

What can you get right now?

I started looking through the plugin directory, did some some searching on Google and asked around. The result was some old diagnostic plugins (like Core Control) that do not seem to be supported anymore, but I am pretty sure they still work though.

You also had some error handling libraries (like the one for raygun.io) that would do something with PHP errors/exceptions (like email them to you or send them to their API).

A good start, but hardly what I was looking for. For example, no plugins could tell you if you had no PHP image editing libraries on your server. WordPress won’t give you an error, but you won’t be able to edit images without it. Often requiring you to debug for a few hours locating the issue.

When WordPress has an error, does it make a sound?

WordPress has a very unique philosophy around errors. This isn’t documented anywhere per say, but, if you are interested in reading about it, Nacin and Otto discuss their view on it in this WordPress hacker thread. At the end of the day, everything is a WP_Error and errors are coded defensively.

You will find that no exception are used in the code and that WordPress either has a fatal error (known as the white screen of death) or will fail silently without you being aware of it. The only errors that generally surface are the ones that will impact the user (Access Denied, Could not delete tag, etc.).

Because WordPress uses WP_Errors for everything, you are left to filter which, as a developer, are relevant to you. I asked around if there was documented hooks or filters where important errors might show up. The answer was that no work had been done to document them so I went deep into the core code to check it out.

Diving for answers

I had a tough time getting that answer. There’s no documentation available so I had to go through the code to find all the error handling code myself. If you’re interested in the process, I looked at every is_wp_error call in WordPress (there’s over 300 in 3.6) and then looked at all the functions and objects that were tied to that error check. That’s a lot of core code, but I was able to get a solid grasp of what was going on because of it.

After that I looked at all the wp_die calls (450+ of them in 3.6), this is a bit more tedious, but the idea was to look at what WordPress considered important enough to terminate itself. Often, they were normal reasons, but the goal was to find the error terminations and their cause.

Finally, I did take a look for new WP_Errors, but they ended not as useful as I had hoped. Critical objects do not always handle errors simply returning a new WP_Error. So while I started with this, it ended up the less useful research path.

What does WordPress do with errors?

As Nacin accurately described, there’s roughly 3 informal classification for WP_Errors. After spending a good two weeks looking at all the error code, I would generally agree with the classification given. He doesn’t really explain what WordPress does with each type so I’ll go through them and explain since this is not documented anywhere.

User Notices

These are specific errors that will surface to the user. They are generally the only WP_Errors that make it to a filter. Their purpose is to alert the user and provide feedback when an error relevant to the user occurs. Plugins can hook in and add their own error message as well. However, these errors are superficial and don’t really alert you to problems going on inside WordPress.

Warnings

These are the devious ones. Warnings are errors that do not directly affect the user experience, but prevent non-critical WordPress features from working. Quick examples are failed HTTP requests with the HTTP API and unable to select an image editor, but there’s plenty of others.

The big issue with these errors is that they very rarely make it to a filter. WordPress will simply return from the function and deal with those internally or dismiss them. As a developer or support individual, they are usually the ones you are the most interested in so it’s a real shame you cannot hook into them.

Failures

Failures are errors that need to be dealt with immediate or WordPress will not function. Most of those are found around the update code (plugins, theme and core), you’ll find some around the database code as well. They will either require user intervention or some form of rollback.

So what about the filters and hooks?

Yeah! What about those!? Like I mentioned previously, I wanted to document useful hooks where errors might show up.

I had initially thought I could use the ‘all’ hook (This hook runs for every hook/filter) to check every hook/filter for WP_Errors. You can’t use it that way. Any function using the ‘all’ hook runs before any other hook/filter function regardless of the given priority so if a plugin returns a WP_Error, you would not know. Bummer.

Besides that, there aren’t a lot of hooks or filters that are relevant if you are looking to check for application errors. Like I mentioned previously, WordPress tends to deal with them internally before they even hit a filter.

Where does this leave us?

As a developer, I hope that you have found the information useful so far. It was incredibly useful for me in getting a better sense of what was going internally in WordPress.

So in the next post, I’ll be introducing the Helthe plugin that I have been working on for monitoring WordPress errors and how I dealt with the limitations I have been discussing today.

Feel free to leave a comment below or discuss on Hacker News.

P.S. If you’d like to keep up to date with my progress with Helthe, leave your email below!

[mailchimpsf_form]

WordPress core as a gateway to better coding

How many of you got into PHP coding because of WordPress?

– Matt Mullenweg

Me and roughly half the room of 200+ attendees raised their hand to that question. That’s pretty incredible mused Matt. He asked that question while answering the larger question asked by 9th grader Lucas Cherkewski which was “How can WordPress be used in an education context to learn coding?”

I have been involved with WordPress for over half its 10 year existence and while I have gone from simple attendee to volunteer to community organizer over that time. I have always been a bit of the crazy one in terms of trying to help the community code better and learn about more advanced programming topics. That’s because I have been coding since I am 7-8 years old so while I learned PHP through WordPress. I had done plenty of programming before then.

Plugin development is like high school

When I was coding in high school, I couldn’t even do something as cool as a WordPress plugin. The web was just starting. I was mostly coding in Basic doing silly things like a text-based RPGs. So I think it’s amazing that someone can solve small problems with a plugin and share it with the world!

For a lot of people, that’s enough. They can solve small problems without knowing a lot of advanced programming concepts. They don’t need to graduate to college level concepts. WordPress has a lot of great APIs that are easy to use and require no advance programming knowledge.

How do you graduate to college level coding?

I did a WordPress meetup on object-oriented PHP recently and I can’t say that I am the best of teachers and a lot of the utility and reason to use object-oriented programming was lost on most because WordPress doesn’t use a lot of objects yet due to its recent PHP4 heritage.

How do I get better at PHP coding?

I don’t get asked that question a lot, but, a few months ago, Michal Bluma asked me how I got into more advanced PHP coding. I told him definitely not with WordPress. I told him I had done a lot of learning looking at the code base Doctrine1, but version1 of Doctrine is mostly discontinued and Doctrine2 is a different beast entirely.

I told him take a look at Symfony, but that’s akin to someone going from high school to college graduate. The knowledge gap is HUGE. The same would be said about Doctrine2, Zend Framework, etc.

Fundamentally, there’s no way easy way for a WordPress developer close the gap.

WordPress core is an ideal candidate for this

WordPress core would be a great place for people to get a taste for advanced programming knowledge. What’s great is that all the APIs already hide most of the internals so, if someone is looking at the core code, it’s because they want to look at it. There’s an opportunity here to allow them to become better coders! Unfortunately, that opportunity is currently wasted.

Core needs stronger coding standard

I am not talking about syntax here, but actual code quality. Core devs need to raise the bar a bit more. I am not talking about super advanced things like Reflection objects or metadata objects, but just better knowledge and use of object-oriented principles. Learning how to do object-oriented programming is really the next step if you want to learn more.

As an example, last night I submitted a ticket to core about the use of final with the WP_Post class. In my opinion, that should have never made it to core in the first place and shows a fundamental misunderstanding of object-oriented coding especially in an open source environment.

What needs to change

Honestly, it would be good if WordPress core at least used some PHP object-oriented best practices. Here are things that would be great for people looking to learn a bit more about being better PHP programmers:

  • Using interfaces where it makes sense to do so like with WP_Widget and WP_Post for example.
  • Severely limit the use of magic methods especially __get and __set
  • Exception handling instead of the white screen of death
  • Proper variable visibility
  • More abstract classes

This is just to name a few. You can find some of these in the code base already, but new code is still being committed where these basic rules are disregarded which is really the point I am trying to get across.

As a core dev, you should be holding yourself up to higher standards because you are indirectly a teacher for thousands of fledgling PHP developpers.

Feel free to leave a comment below or discuss on Hacker News.

My first open source contribution: Chronos

I have been involved with open source for a couple of years now. However, my involvement has always revolved around organizing events for the Montreal WordPress community. Except for a handful of pull requests and small jQuery plugins, I never created anything I felt would be useful to share. I am slowly trying to change that.

While I have a huge respect for Automattic as a company, I can’t think of a company that I admire more than GitHub. One of their mantras I always wanted to follow was open source (almost) everything. So since I started my current project, I have tried to keep my code as modular as I could so I could open source most of it if I could.

I didn’t really have a great opportunity till recently when I started needing recurring jobs that I could manage in PHP. I needed something that could manage both crontab like Whenever, but also handle recurring jobs programmatically a bit like wp_cron does. I looked and couldn’t find a library or set of libraries to do this.

I felt this was a good opportunity to try to get something out so I made one and I am open sourcing it today as Chronos. I’ll have a followup Symfony2 bundle available in a few days.