Last active
December 8, 2015 15:55
-
-
Save salcode/72d43b2104b4ef8fb75f to your computer and use it in GitHub Desktop.
Shortcode to output the value of a constant. e.g. [fe_constant_test constant="DB_HOST"]
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 | |
/** | |
* Shortcode [fe_constant_test], which takes one parameter called "constant" | |
* This shortcode then outputs the value of that constant. | |
* I find this helpful when testing the value of constants with a complex | |
* constant assignment (e.g. using .env) | |
* | |
* Example usage: [fe_constant_test constant="DB_HOST"] | |
* outputs the following on my local development environment | |
* Constant DB_HOST is defined as "localhost" | |
* | |
* Sal Ferrarello | |
* http://salferrarello.com/ | |
* http://twitter.com/salcode | |
* | |
* This can be added by navigating to the `mu-plugins` directory and running the following from the command line | |
* curl -O https://gist.githubusercontent.com/salcode/72d43b2104b4ef8fb75f/raw/b741e2d5b2c45da80286e6fd772a59f414dfa8fa/shortcode_fe_constant_test.php | |
*/ | |
add_shortcode( 'fe_constant_test', 'shortcode_fe_constant_test' ); | |
function shortcode_fe_constant_test( $atts ) { | |
shortcode_atts( | |
array( | |
'constant' => false, | |
), | |
$atts, | |
'fe_constant_test' | |
); | |
if ( false === $atts['constant'] ) { | |
return 'no constant was specified as shortcode parameter'; | |
} | |
$constant_name = $atts['constant']; | |
if ( ! defined( $constant_name ) ) { | |
return "Constant {$constant_name} is undefined"; | |
} | |
$constant_value = constant( $constant_name ); | |
return "Constant {$constant_name} is defined as \"{$constant_value}\""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment