-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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