• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP Dev Life

A Journey through WordPress

  • About Jay
  • Dev Tools
  • Contact Us

Archives for May 2019

Getting Started with Unit Tests

May 31, 2019 by Jay

What is Unit Testing?

Unit Testing is the process of writing code to ensure that other code functions with its expected behavior.

Why should I write tests?

Writing tests for your code can help drive the development of your code. This is referred to as Test Driven Development (TDD). Recently I was working on a method that sanitizes the input of a form field, and I needed this function to perform different conditionals and formatting. Instead of dumping a variable out and refreshing the page, to see the output, I was able to write test cases with what I expect the output to be, and the actual output that was returned. This allowed me to speed up the development process, and easily iterate through each condition I needed to sanitize against, and it ensured that the code returned what we wanted it to return.

Let’s Get Started

I utilize a custom Docker environment for my local development that has a separate container for PHP, MySQL, NGINX, and I’ve got a few others in there to help mimic my production environment but those aren’t really needed. I’m able to open up an SSH connection into the containers that have my site mounted, and I have access to WP-CLI. The only real requirement here is that you’ve got WP-CLI, but this walk through assumes you’re using something similar, you’ve got access to /tmp, and WP-CLI. Additionally you will need Composer, a PHP package manager, installed as that is how we’ll be requiring our vendor dependencies like PHPUnit.

If you’ve already got a plugin or theme setup but no tests, WP-CLI has the scaffold command which has a few options to scaffold just tests for a plugin or theme.

$ wp scaffold theme-tests <plugin-name>
OR
$ wp scaffold plugin-tests <theme-name>

The two above WP-CLI commands will scaffold all of the necessary files to begin writing tests, as well as providing a test-sample.php file.

This creates a phpunit.xml.dist file as well as the tests folder which contains the test-sample.php file.

Filed Under: Programming, WordPress

WordPress REST API

May 17, 2019 by Jay

What’s an API?

An API is an interface that allows applications to use, create or modify data from another application.


What’s the WordPress REST API?

The WordPress REST API takes information from the database and serves it in JSON format. This includes, posts, media, pages, and users and others. We’ll cover the Core endpoints further down.

The REST API can have two different structures depending on how permalinks are setup. On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For a site using the default permalink structure the REST API full URL would then be http://example.com/?rest_route=/wp/v2/posts/123

Plugins can extend this feature to generate their build their own API for use. A good example of this is the Woocommerce API, which we touch on below.


What is a route?

A route, in the context of the WordPress REST API, is a URI which can be mapped to different HTTP methods. The mapping of an individual HTTP method to a route is known as an “endpoint”. 

ResourceRoute
Posts/wp/v2/posts
Revisions/wp/v2/revisions
Categories/wp/v2/categories
Tags
/wp/v2/tags
Pages/wp/v2/pages
Comments/wp/v2/comments
Taxonomies/wp/v2/taxonomies
Media/wp/v2/media
Users/wp/v2/users
Post Types/wp/v2/types
Post Statuses/wp/v2/statuses
Settings/wp/v2/settings

HTTP Request Methods

Every time we go to a website, we’re making multiple HTTP requests. One to the server and then that sends a response which tells us we need to make more requests to get assets so that the browser renders the page properly. That is an example of a GET request. Whenever a slider changes, and triggers a request to admin-ajax.php, those are POST requests. Below is a table of different request methods availbab

GET Read data from an endpoint
POST Send data to an endpoint to create new data
PUTUsed for updating already existing data
DELETEDelete data from an endpoint
OPTIONS Used to determine which methods an endpoint accepts

Caching

GET requests will be cached per normal varnish rules.

POST requests will always be uncached.


Custom Routes

It is possible for plugins and themes to extend the WordPress REST API with their own routes. Woocommerce uses /wp-json/wc/v3 as their endpoint. They then have routes for products, orders, customers, coupons and more. Most of these requests will require authentication with a Woocommerce API key which can be generated in their Woocommerce settings page in their dashboard.


Responses

The WordPress REST API returns data in JSON format. This can then be consumed by other applications to process the data. When you load the REST API in your browser it will look similar to this:

[{"id":95,"date":"2019-05-31T15:40:16","date_gmt":"2019-05-31T15:40:16","guid":{"rendered":"http:\/\/wpdev.life\/?p=95"},"modified":"2019-05-31T15:40:16","modified_gmt":"2019-05-31T15:40:16","slug":"53w5","status":"publish","type":"post","link":"https:\/\/wpdev.life\/53w5\/","title":{"rendered":"53w5"},"content":{"rendered":"\n<p>weqtwetq<\/p>\n","protected":false},"excerpt":{"rendered":"<p>weqtwetq<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"wpe_featured":["yes"],"_links":{"self":[{"href":"https:\/\/wpdev.life\/wp-json\/wp\/v2\/posts\/95"}],"collection":[{"href":"https:\/\/wpdev.life\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpdev.life\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpdev.life\/wp-json\/wp\/v2\/users\/1"}],"replies":........}]

You can make this look prettier using the JSONViewer Chrome Extension.

All of the information about a post or page can be obtained through the REST API. This is what allows developers to create phone applications, or run Headless WordPress sites, where the front end is a Node or React application powered by the WordPress REST API.

Filed Under: Programming, WordPress

Primary Sidebar

Learn Linux

Use LinuxAcademy to learn Linux, AWS, or other Cloud technologies.

Find Something

Categories

Recent Posts

  • Using withSelect for WordPress Block Components
  • Gutenberg: Attributes vs State
  • Getting Started with Unit Tests
  • WordPress REST API
  • Authentication and the REST API

Archives

  • November 2019
  • May 2019
  • January 2019
  • July 2018
  • February 2018

Copyright © 2025 · Wp Devlife on Genesis Framework · WordPress · Log in