Practical WordPress Development

Author: Tom (Page 1 of 390)

How To Fix TypeError: Cannot Read Property

If, at some point, you’re working on some front-end code using NPM and when you attempt to install a package, you get a message that resembles something like this (you’re mileage obviously may vary):

TypeError: Cannot read property 'properties' of undefined
...
at Object.<anonymous> ([...]/node_modules/webpack/bin/webpack.js:156:2)

That is, the primary problem being: TypeError: Cannot read property ‘properties’ of undefined

The best solution I’ve found to the problem is to remove webpack and the webpack-cli and reinstall it using the --save-dev directive.

Continue reading

De-coupling Domain Logic in WordPress

Remember that WordPress uses the event-driven design pattern and although we often refer to actions and filters, the concept comes down to hooks. The flow of control through the program goes something like this:

  1. Execute the program,
  2. Whenever the program comes upon a hook (in WordPress, we’ll see do_action or apply_filters), iterate through all of the registered hooks,
  3. Return control back to the program,
  4. Execute until the end.

This isn’t completely different from the Publisher/Subscriber Pattern (or PubSub, for short) but there’s a key difference: The Event-Driven Pattern simply signals that something has happened and if there are hooks, they will fire. The PubSub Pattern will tell a registered subscriber to do something.

Anyway, back to hooks in WordPress. Keeping the two concepts of hooks that we have may be most easily done by thinking of them like this:

  • Actions are for doing something,
  • Filters are for processing data.

If you’re looking to approach WordPress development in an object-oriented fashion, tightly coupling your code to WordPress core by registering your classes via hooks to the core application is not a good idea.

In other words, don’t register your business logic with WordPress. Keep them separate. Here’s a litmus test for if your work is tightly coupled with WordPress: If you can’t run a unit test against your class without loading WordPress, it’s tightly coupled.

So what’s the solution? Delegation.

Continue reading

Using PHPCS with Oh My Zsh

If you’re using macOS Catalina (or later depending on when you’re reading this post) and you’re also running a combination of:

And you’re using the latter installed at a global level but your IDE nor your terminal sessions are able to pick it up, make sure that you’ve updated your path.

Remember:

Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration.

Oh My ZSH

This means if you’re going to make any changes to your path, you need to update ~/.zshrc.

Continue reading

On Functions and Must-Use Plugins

I’ve been working on a small project, more of a web application than a site, that’s required the development of both a custom-theme and tightly coupled, but very specific functionality.

This is a very narrowly focused project (about which I’ll likely talk about at some point in the future) but in working on it, it’s forced me to get back into the theme development aspect of WordPress development a little bit.

No, I’m not doing any design – thankfully – but I am having to work on theme customizations from a functional perspective. In doing this, though, it’s had me revisit the required functions.php and some considerations I’ve never had before.

Furthermore, it’s caused me to look more deeply at the use of mu-plugins and ask when they are necessary and why I haven’t used them more in the past (or even when one would truly need to use them).

So I’m going to wax poetic about that a bit.

TL;DR
Functionality tied directly to the theme and WordPress core goes into functions.php. Domain-logic that required by the entire solution to work goes in a must-use plugin.
Continue reading

Don’t Develop Development Tunnel Vision

In previous posts, I’ve talked about the idea of focusing on an area and going deep rather than wide. This is personal preference, of course, but it’s mine, nonetheless.

Over the last year, though, one of the byproducts that I’ve found is the longer you stay in a given industry, the more common certain problems become. (This shouldn’t come as a surprise as this is precisely why we have design patterns.)

But the thing about doing this is that you develop a sort of tunnel vision for ways to solve problems.

Credits

Case in point: Recently, I was tasked with needing to develop some functionality that was going to parse markup and convert it into a slightly different format.

Continue reading
« Older posts

© 2020 Tom McFarlin

Theme by Anders NorenUp ↑