Using Ray in WordPress

There are several ways to install Ray in WordPress.

#Global installation

The easiest ways is the global installation. This will make the ray() function available in any WordPress (and PHP file on your system).

Issue these commands to install the global-ray package:

composer global require spatie/global-ray
global-ray install

You can now use the ray() function and all of its framework agnostic capabilities. In each WordPress app you can also use these functions:

  • dump($variable): dump any kind of variable to the CLI.
  • dd($variable): dump any kind of variable to the CLI and terminate the script.

To use the WordPress specific capabilities of Ray, you should install wordpress-ray into the WordPress app.

#Manually cloning the repo

Inside the wp-contents/plugins directory run this command

git clone git@github.com:spatie/wordpress-ray

#Installing Ray via the WordPress admin interface

Ray is also registered as a plugin on WordPress.org. In the admin section of WordPress, go to "Plugins" > "Add New", and search for "Spatie Ray".

screenshot

Install and activate the plugin.

#Must use plugins

By default WordPress loads your plugins in the following order:

  • Checks for any must-use plugins directory (default = /wp-content/mu-plugins).
  • Then, if you're running a multisite installation, it checks for plugins that are network-activated and loads those.
  • Then it checks for all other active plugins by looking at the active_plugins entry of the wp_options database table, and loops through those. The plugins will be listed alphabetically.

If you wish to debug your plugins within the Ray app it is recommended that you install the plugin into your /wp-content/mu-plugins directory. Further details on Must Use Plugins can be found here:

To install, inside the wp-content/mu-plugins directory run this command:

git clone git@github.com:spatie/wordpress-ray

Next, from the just created wordpress-ray directory, run this command:

composer install -o --no-dev

You'll then need to create ray-loader.php within /wp-content/mu-plugins and include the following code:

require WPMU_PLUGIN_DIR.'/wordpress-ray/wp-ray.php';

#Setting the environment variable

When developing locally you should have WP_ENVIRONMENT_TYPE set as local in your wp-config.php otherwise Ray won't work.

define( 'WP_ENVIRONMENT_TYPE', 'local' );
Help us improve this page