A Fallback for Ray

As I mentioned in a previous article, I really like using Ray for debugging as I develop WordPress projects. However, one thing that can be easy to do is to forget to remove all of your calls to ray() and upload to a production site where the Ray plugin is not installed. Then… ack! An error because of a call to an undefined function. In my experience, one thing that makes finding all of the calls to ray using search in my IDE is that it shares text with another highly-used PHP function. You might have heard of it… array().

Of course, being careful and having code reviews before deployment would go a long way to never having this happen. However, I work alone on many projects and although I try to be careful, this error has bit me before. I decided to write a quick little function for my theme’s functions.php file as a safeguard against this. Here is my simple code:

 * A backup to create a ray function if troubleshooting code is in place on a site without the Spatie Ray plugin.
if (!\function_exists('Spatie\\WordPressRay\\ray')) {
	class Ray {
		public function __construct( ...$args ) {
		public function __call( $meth, $args ) {}
		private function scc_send_ray_alert() {
			$site_name   = get_option( 'blogname' );
			$admin_email = get_option( 'admin_email' );
			$email_content  = "A call to the ray() function was made on {$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}. However, the Spatie Ray plugin is not installed and activated.";
			wp_mail( $admin_email, "ray function called on {$site_name}", $email_content );
	function ray() {
		return new Ray;

I only want to create my own version of the ray function if it doesn’t already exist. The first line of my code checks for the existence of this function. If ray does not exist, I set up my own code that will send me an email to alert me that there is a call to the ray function on a site where the Ray plugin is not installed and active. This will prevent any PHP errors from occurring and let me know to clean up the code.

Another solution would be to install the Ray plugin on the production site but I always try to limit the number of plugins on production sites. I also don’t want to run unnecessary code on the live site. On the live site the debugging information is not being sent anywhere and is of no utility. So having the plugin running and possibly parsing debugging calls seems wasteful.

Leave a comment

Your email address will not be published. Required fields are marked *