Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Last active July 25, 2017 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrbobbybryant/808749a62583d0789317 to your computer and use it in GitHub Desktop.
Save mrbobbybryant/808749a62583d0789317 to your computer and use it in GitHub Desktop.
Allows you to dynamically determine if your code is being used in a plugin or a theme
<?php
function wp_code_context() {
$theme = strpos( __DIR__, 'themes' );
return ( $theme ) ? 'theme' : 'plugin';
}
function get_directory_by_context() {
$context = wp_code_context();
switch( $context ) {
case 'theme':
$local_dir = get_template_directory();
break;
case 'plugin':
$local_dir = plugin_dir_path(__FILE__);
break;
default:
$local_dir = false;
}
return $local_dir;
}
function get_directory_url_by_context() {
$context = wp_code_context();
switch( $context ) {
case 'theme':
$local_url = get_template_directory();
break;
case 'plugin':
$local_url = plugin_dir_path(__FILE__);
break;
default:
$local_url = false;
}
return $local_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment