TL;DR: We’ll see what Ray looks like when rendering data, data structures, and other information in the context of a custom WordPress plugin. We’ll also see how to leverage some of its built in functionality for making data structures much more readable from how we’ve historically been able to do so.


📝 A Note About the Ray Plugin

When it comes to actually using the Ray application within the context of WordPress, I’d like to bring some clarity to the different ways in which it can be installed.

  1. Ray can be installed as a Composer dependency which is what we did in the previous article.
  2. Ray can be be installed a plugin via the WordPress Plugin Repository.
  3. Ray can be installed as a must-use plugin by cloning the repository from GitHub, placing it into your mu-plugins directory and then updating environmental variables as per the documentation.

All of these are viable options. I prefer to use the first option because I’m a fan of Composer and managing my dependencies that way so this guide will be following that approach.

If you opt to use any of the approaches, great! This series, however, will not offer guidance on those methods.


⚠️ If you’ve not already set up your environment, please read the previous post and make sure you have the free version of Ray installed.

Rendering Data and Data Structures

Before getting too deep into what everything Ray can do, it’s important to first make sure it’s working in the context of a basic plugin. In this article, we’re going to write a plugin and look at some of the things Ray can do when rendering data and returns from simple function call.

I’ve already written about how it’s more than a way to have pretty print_r and var_dump calls or echo statements, but we’ll look at those first. Then we’ll take a look at just a couple of the more powerful things it can do for us.

A Sample Plugin

Recall in the first post, we setup a basic plugin bootstrap file; however, it doesn’t really do anything. Let’s change that.

First, open the file and add an anonymous function that’s hooked into the_content filter.

This filter is used to filter the content of a post after it is retrieved from the database and before it is printed to the screen.

code reference

It will do the following:

  1. If we’re not on a single post, return the specified content.
  2. Otherwise, have room for some custom functionality.
  3. Return the content back to WordPress.

This is what the code should look like:

Note that this will not do anything on the front-end of the website. But, before writing any more code, make sure the plugin is activated and that you can browser the site and load a single post without seeing any errors.

If so, let’s continue.

Writing Output to Ray

Let’s look at a simple example first. We’ll do this by using the default ray function. After installing the dependency, this function is globally accessible and is the easiest way to output information into the Ray application.

  1. Open the Ray application
  2. Add the following line of code to the anonymous function we shared above.

Next, load your site in your browser. If everything is set up properly, this is how Ray should look when you’re browsing the standard list of articles:

But if you visit a page and you’re not logged into the site or not logged in as an administrator, you should see this:

Next, login to your site as an administrator and then refresh the single post page. Once done, you should see this:

Assuming all’s gone well, not bad right? You’re outputting the result of current_user_can into Ray and seeing the results.

Rendering Data Structures

Now that we’ve seen how easy it is to simply print the output of a given function to Ray, let’s look at what it’s like to print out something more advanced.

  • For example, what if we’re to look at the current WP_User information?
  • Or what if we want to see some of the user’s metadata?
  • What if we want to see an array cleanly represented in an easy to read format?

WP User Information

Let’s take a look at the current user’s information. Remember, we need to be logged into WordPress to do this so. Further, we’ll be using the wp_get_current_user API function.

Will set the current user, if the current user is not set. The current user will be set to the logged-in person. If no user is logged-in, then it will set the current user to 0, which is invalid and won’t have any permissions.

code reference

First, update your anonymous function to look like this:

Next, refresh a single post while logged in as an administrator and you should see something like this rendered in Ray:

Note that I manually expanded the roles attribute for the sake of showing a little more data in the screenshot. Still, it’s neat, right?

Review User Metadata

To get the user’s metadata, we get use the current user’s ID and the get_user_meta API function to read any piece of information in the database.

Retrieve user meta field for a user.

code reference

For the sake of using something slightly out of the box, let’s check and see if I have opted to display the administration menu (you know, the top menu bar that sits at the top of the browser when you’re logged into WordPress) render on the front-end of the browser.

To do this, add the following code to the plugin:

And this is what Ray should render:

Nice, right?

Good Looking Data Structures

Before we conclude this article, I’d like to show how we can take a data structure such as an array and render it in much more readable fashion than perhaps we’re used to doing with the other tools at our disposal.

It’s be easy to use one of the attributes on the WP_User object above, such as caps or roles or allcaps so we’ll stick with something simple for this article.

Let’s look at allcaps. It’s a relatively large set of data to browse through at 62 elements. Let’s read the array, shorten it to the first 15 elements, and then render that data cleanly in Ray.

Here’s how to do it:

Specifically, notice the table function renders a significantly better version of an array than we’re used to seeing with some of our other options for looking at data:

Of course, there’s more we can do but this is sufficient for where we we are in the series at this point.

🔖 Remember The Tag

Recall I’m using the develop branch of the repository for each article. After the article, I’ll merge the contents of develop into main and then I’ll tag the version with the date the article was publishing. 

The tag for this article will be 12012022.

We’ll continue to look more into what Ray can do for us within WordPress in the next article.


☕️ Enjoying this guide? Consider Buying Me a Coffee to support these longer form articles!