One of the challenges that theme and plugin developers have to face is making sure that their code works regardless of the directory in which their project is installed.

Case in point: for the past couple of weeks, I’ve been working on a number of WordPress-specific projects each of which features some heavy client-side functionality.

The challenge is including the required JavaScript files regardless of where the theme is installed.

The way that I’ve been accomplishing this is to register and enqueue my scripts by adding a theme_path query string variable to the script’s path:

wp_register_script('admin', get_bloginfo('stylesheet_directory') . '/javascript/admin.js?theme_path=' . get_bloginfo('stylesheet_directory'));


Then, in the JavaScript, I’ll parse out the query string to grab the path:

var sThemePath = '';
$('script').each(function() {
  if($(this).attr('src') !== undefined) {
    if($(this).attr('src').indexOf('theme_path') > -1) {
      sThemePath = $(this).attr('src').split('theme_path=')[1].split('&')[0];
    } // end if
  } // end if

Then I’ll typically decode and it proceed as usual:

$.get(decodeURIComponent(sThemePath) + '/required-file.php', function(data) {
  // for example...

I’ve used this a couple of times now with good results.


    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.