Last active
July 25, 2017 08:40
-
-
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
This file contains hidden or 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