How to fix a blank events page when using the WordPress Eventbrite API plugin

Integrating Eventbrite

I was recently working on a site, where the client wanted to display events from Eventbrite directly into a page on their own website, rather than having visitors leave to the Eventbrite website. To do this, I used the WordPress Eventbrite API plugin.

A blank page!

However, I hit a pretty major problem. No matter what I did, I just got a blank page, even though I’d supplied an eventbrite-index.php template file in my theme. Yet when I installed the site on my staging server, everything worked just fine. I needed to work on the site locally, so it’s important I got this working.

After looking through the Eventbrite API code, it turned out it was an issue with running on Windows, and the way the WordPress esc_url() function works.

Specifically, the Eventbrite_Templates class in inc/class-eventbrite-templates.php is responsible for finding the correct template file from the plugin or theme. By providing our own template file, we can style the event list the way we want.

esc_url() works differently on Windows

On line 156 of the file we have the following line of code:-


$template = esc_url( get_stylesheet_directory() . 
'/eventbrite/eventbrite-index.php' );

Here it’s passing the template file path into the esc_url() to escape it. Now this is fine on Linux, since file paths begin with a ‘/’ and indeed the esc_url() function makes a special case for this and lets it through. But of course, on Windows, file paths begin with C:\, and esc_url() considers this invalid, so the path is discarded, and an empty string is returned. So we get an empty page!

Fixing the problem

To workaround this problem, we can use a couple of action filters. I stress that this is very much a workaround for local development only, and involves a hack, which is why it only runs on localhost and only if a special debug constant is set. Here’s the code:-


// only run the workaround if debug mode is active
if (mytheme_debug()) {

  add_filter( 'kses_allowed_protocols', 'mytheme_add_windows_drives' );
  add_filter( 'stylesheet_directory', 'mytheme_normalize_stylesheet_directory' );

}

// hack - add "C" as a protocol to allow local a windows style path through the esc_url() function
// Replace "C" as required for different drive letters.
function mytheme_add_windows_drives( $protocols ) {

  $protocols[] = 'C';
  return $protocols;

}

// normalise the stylesheet directory
function mytheme_normalize_stylesheet_directory( $stylesheet_dir ) {

  return wp_normalize_path( $stylesheet_dir );

}

// return true if debug mode is activated
function mytheme_debug() {

  // only run on localhost and if the debug flag is set
  if ( $_SERVER["HTTP_HOST"]=="localhost" && defined('EVENTBRITEAPI_DEBUG') && EVENTBRITEAPI_DEBUG==true ) return true;
  return false;

}

To activate debug mode on localhost, add the following line to wp-config.php

  define('EVENTBRITEAPI_DEBUG', true );

And that’s it. The Eventbrite API plugin is a fantastic bit of software, so I hope this is useful to anyone else using it on a Windows development machine.

Leave a Comment

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

Scroll to Top