In the first post in this [two-part] series, I talked about the idea of what it means to set a goal. Specifically, I said it was more nuanced than just setting a goal and aiming for it. Instead, I said I thought of doing this:

  1. set a goal,
  2. make a plan.

And, if you read the first, you know that I gave an example outside of programming (because I tend to do that sometimes).

But why not also look at what this would look like regarding programming? I mean, the whole point of the site is to talk about how to handle WordPress development from a practical perspective.

And this seems like something that intersects with that whole idea, right?

How to Set a Goal: Programming

From the outset, I think it’s important for me to clarify a single point:

I view programming as very much taking a problem and breaking it down into components where each component may be seen as a goal. And if I use the strategy above, then there’s a plan that needs to be formulated for how to achieve a said goal.

If you take that just one step further, then it means we’re also looking at what it means to take the plan and break it into incremental steps to solve part of a problem.

And the reason I distinguish part of a problem over a single problem is because the statements and instructions we write eventually – if done correctly, of course (which rarely happens the first time, but I digress) – work up to creating blocks of code that formulate functions that compose a class that eventually work together with an existing system to solve the problem.

Granted, maybe you don’t use OOP and if that’s the case, then forget the class part of the statement above. Everything else still rings true, doesn’t it?

An Example

For this post, let’s assume that a person (read: a customer or client) has come to you and asked you to write some functionality that accepts user input and saves it to the database.

In the context of WordPress, it sounds easy enough, right? Perhaps it would go something like this:

  1. Create the settings page,
  2. Add the proper nonce value,
  3. Save the value

But there are more nuances to that, and that’s where making a plan comes into the picture. More specifically, this is where we take the goal that we have in mind and break it down into more manageable pieces.

How to a Set a Goal: Programming

For the sake of brevity, I’m not going to be sharing any code in this post – but I think it’s important to take the steps above and have a plan for each of them. So I’d approach it like this (and this does get a bit lengthy).

  1. Create a settings page.
    1. This can be done via the Settings API, add_menu_page, or even add_submenu_page (if that’s what the project requires).
    2. Write the page content,
    3. Display the form for accepting user values including the form submission button (and perhaps a Clear or Reset or Delete button).
      1. Use JavaScript (or even a simple post-back to clear the form).
      2. Use form submission for Delete to remove the value but only if you use the following steps.
  2. Add the proper nonce value.
    1. This can be done via the wp_nonce_field function (though there are other functions).
    2. Create a function to verify that the user can save the value upon form submission.
      1. This includes checking the user’s permissions in WordPress,
      2. And it includes checking the value of the nonce value.
  3. Save the value.
    1. If the user has permission and the nonce value is accepted, then sanitize and save the value.
      1. Optionally incorporate a success message that the user can see.
      2. Retrieve the value, validate it, and populate it in the form field.
      3. Optionally, if the value is empty, delete the value from the database.
    2. If not, do not save the value (or even touch the value in the database).
      1. Add an error message that the user cannot save the value and optionally provide a reason (depending on the data and the reason).
      2. Do not display anything in the form field other than what may have existed before.

Here’s the thing though, each one of these steps above can translate into a commit which means that it’s easy to rollback a problem, fix it, and then update it.

How to Set a Goal: Commits

This gets more into source control management, so for all intents and purposes, I’ll leave it at that.

Have a Goal, Make a Plan

So that ends the second part of these two posts (again, there’s the link to the first if you’re interested).

Ultimately, the goal was two-fold:

  1. help those of you who struggle with setting goals and achieving them (which I believe it many, many of us),
  2. and to help identify how to make a plan to achieve a goal by breaking it down into more manageable steps.

Next week, in addition to my regular programming-related articles, I’m also looking to share a few additional resources that I’ve found useful as it relates to certain mobile apps and desktop apps.

For now, though, this wraps up this series and sets the site back up to return to the usual content related to practical WordPress development.