Using the PHP Interpreter When Working With WordPress

Recently, I’ve been working on a project that requires a lot of work with dates. Depending on the nature of the work, there are times in which this can be easier than others.

Using Visual Studio For Date Comparison

On the one hand, if you’re making a simple comparison between, say, the values of two months then that’s not a big deal. On the other hand, if you’re dealing with months, days, years, the current date, timezone offsets, and different formats, then it can be a bit more complicated.

The point of all of the above, though, is not about strictly working with dates. Instead, it’s about the challenges that come with needing to write, test, and work with source code.

And if you’re in the habit of writing code, testing it in a browser, doing some form of debugging or dumping information onto a screen, and then repeating the process, there are easier ways to deal with this.

Using The PHP Interpreter

I’d venture to say that many people who work with PHP in a professional capacity are familiar with using the PHP interpreter, it’s something that is easy to forget when you’re working with web applications because you have the browser as a view and as a way to evaluate your code.

But if you have PHP installed, you have the interpreter installed. Assuming PHP is in globally available on your system, you can access it via the command-line by typing:

$ php -a

From here, you’ll be able to write PHP code and evaluate it whenever you begin interacting with the interpreter.

Using the PHP Interpreter

Though this can be a bit cumbersome when working with classes (and not something I recommend), but issuing commands and evaluating statements work great.

So what does this have to do with WordPress?

The WordPress Database

Bringing the post full circle, how might one work with date comparisons using information in the WordPress database for a given post and another date?

Perhaps you could:

  • grab the value of the database column,
  • store it in a string in the terminal,
  • create your date value in the terminal,
  • and perform the necessary operations for your testing.

Again, it’s not the only way to deal with this kind of stuff, but it’s one way. And having the PHP interpreter available can be a big help when you want to test some code on a proverbial paper napkin sketch essentially.

1 Reply to “Using the PHP Interpreter When Working With WordPress”

  1. Also, if you need to do the equivalent in a fully-bootstrapped WP environment, WP-CLI has you covered:

    $ wp shell

    wp> $product = new WC_Product(475);

    => object(WC_Product)#11745 (12) {

    [“object_type”:protected]=>

    string(7) “product”

    [“post_type”:protected]=>

    string(7) “product”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.