A simple wordpress shortcode function gets the querystring key => value pair from the URL and outputs the value onto the page where the shortcode is placed. I set the parameter in the shortcode to determine which key to call e.g. [urlparam param="username"].
Unfortunately, if there is no querystring or if the querystring key is not present then the raw shortcode displays on the page rather than rendering any result or displaying nothing. How would I edit the shortcode function so that nothing displays if there is no querystring or if the key being looked for is not there?
function urlparam_function( $atts ) {
extract( shortcode_atts( array(
'param' => 'param',
), $atts ) );
return $_GET[$param];
}
add_shortcode('urlparam', 'urlparam_function');
remove $_GET
use only $param
like below:
function urlparam_function( $atts ) {
extract( shortcode_atts( array(
'param' => 'param',
), $atts ) );
return $param;
}
add_shortcode('urlparam', 'urlparam_function');
If the querystring key is not empty, return the querystring value. Otherwise return null
.
Returning null
results in nothing being displayed.
function urlparam_function( $atts ) {
extract(
shortcode_atts(
array(
'param' => 'param',
),
$atts
)
);
return !empty($_GET[$param]) ? $_GET[$param] : null
}
add_shortcode('urlparam', 'urlparam_function');