Skip to content

Instantly share code, notes, and snippets.

@dharmeshtops
Forked from mrbobbybryant/wp_code_context.php
Created July 25, 2017 08:40
Show Gist options
  • Save dharmeshtops/201a35de3b3dc5b4e566bdbfb4f6d35c to your computer and use it in GitHub Desktop.
Save dharmeshtops/201a35de3b3dc5b4e566bdbfb4f6d35c 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