Skip to content

Instantly share code, notes, and snippets.

@salcode
Last active December 8, 2015 15:55
Show Gist options
  • Save salcode/72d43b2104b4ef8fb75f to your computer and use it in GitHub Desktop.
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"]
<?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