Use theme constants in plugin?

I’m creating a plugin version of the Roots Theme function additions – to make it a bit more portable, but have run into a snag.

Part of what Roots does is add htaccess rules based on the current theme information — like so:

// only use clean urls if the theme isn't a child or an MU (Network) install
if (!is_multisite() && !is_child_theme() && get_option('permalink_structure')) {
  add_action('generate_rewrite_rules', 'roots_add_rewrites');
  add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
  if (!is_admin()) {
    $tags = array(
      'plugins_url',
      'bloginfo',
      'stylesheet_directory_uri',
      'template_directory_uri',
      'script_loader_src',
      'style_loader_src'
    );

    add_filters($tags, 'roots_clean_urls');
  }
}

So, it’s basically checking to make sure that the current setup is not a multisite environment, not a child theme and that the permalink structure has been updated from the defaults. It seems like the is_child_theme() is not being called correctly, mainly because I get the following error on the frontend:

Notice: Use of undefined constant TEMPLATEPATH - assumed 'TEMPLATEPATH' in /Users/studio/Sites/base_wp/wp-includes/theme.php on line 17

Notice: Use of undefined constant STYLESHEETPATH - assumed 'STYLESHEETPATH' in /Users/studio/Sites/base_wp/wp-includes/theme.php on line 17

So, from what I can gather, it’s the wp-includes/theme.php file that is not included in plugin files (which makes senses).

I’ve been banging my head against the wall with this one, but am trying to figure out the cleanest way possible to add in those constants to the plugin itself -> should I just include that theme.php file? Thanks.

UPDATE: Looks like I can use functions that are in wp-includes/theme.php (like get_themes()) so I know that’s not the issue. hm…

1 Answer
1

TEMPLATEPATH and STYLESHEETPATH will be deprecated in the near future. It is not safe to rely on these constants. Use get_template_directory() and get_stylesheet_directory() instead.
And wait until the init hook (or after_setup_theme before you use them to be sure all needed files are loaded.

Example for a plugin:

add_action( 'after_setup_theme', 'wpse_50359_set_plugin_basics' );

function wpse_50359_set_plugin_basics()
{
    if ( is_child_theme() )
    {
        // do something awesome …
    }
}

Leave a Comment