Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a settings API class which I'd like to be able to drop into a plugin, wpmu plugin, theme, or child theme directory, and have it just work. My issue is that I'm having trouble figuring out how to determine the URIs for including scripts and styles on the fly. I'd like this to work no matter how the site's WP install is set up, so it should work where ever the wp-content, plugins, mu-plugins, and themes directories are located.

I found this post which involves something similar, but it's not quite the same, since it's dealing with paths and not URIs. Also, (frankly), I'm finding it a little challenging to wrap my head around, and I'd like to avoid going down that rabbit hole if my carrot isn't going to be there.

Any advice on how to approach this?

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

For plugins it is:

$plugin_url = plugin_dir_url( __FILE__ );

This works even if wp-content is on a separate domain. You may get problems if your class is in a subdirectory of the plugin – you should probably make the __FILE__ a parameter for a function:

public function get_plugin_url( $base = __FILE__ )
{
    return plugin_dir_url( $base );
}

You could also check if the parent directory of your class is the plugin directory and adjust the path accordingly.

Similar, for themes use get_stylesheet_directory_uri(). It works for parent and child themes.

share|improve this answer
    
I understand how to get the URIs for the appropriate location (plugin/theme/child theme), but I wasn't sure how to automatically determine if I should use plugin_dir_url( __FILE__ ) or get_template_directory_uri(), etc. I think you've got me on the right track with suggesting that I check the parent directory. This class will likely be in its own subdirectory, so I think I'll add a variable for that, and then work my way up to figure out where I'm at (checking against WP_PLUGIN_DIR/WPMU_PLUGIN_DIR/get_theme_root() along the way.) Thanks! –  goto10 Feb 21 '12 at 1:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.