Skip to content

Instantly share code, notes, and snippets.

@n8kowald
Last active Mar 21, 2022
Embed
What would you like to do?
If elseif else shortcode example
<?php
// See this comment: https://www.nathankowald.com/blog/2019/06/wordpress-shortcode-if-elseif-else-statements/comment-page-1/#comment-222167
/**
* Add this code into your theme's functions.php file (or custom plugin).
*/
add_filter( 'query_vars', function( $vars ) {
// Add any other $_GET params you want to use.
$vars[] = 'paramName';
return $vars;
});
function param_is( $param_name, $val ) {
return $val === get_query_var( $param_name );
}
add_filter( 'if_elseif_else_shortcode_allowed_callables', function( $whitelist ) {
$whitelist[] = 'param_is';
return $whitelist;
});
/* Shortcodes to use:
[if param_is paramName value1]
text option #1
[elseif param_is paramName value2]
text option #2
[else]
text option #3
[/if]
*/
@devidw
Copy link

devidw commented Mar 21, 2022

There should be a tailing ; after the last add_filter() call, when we are not using a closing ?> tag: https://stackoverflow.com/a/29284131/13765033

@devidw
Copy link

devidw commented Mar 21, 2022

When you don't want to whitelist every GET parameter:

function param_is( $param_name, $val ) {
	return isset($_GET[$param_name]) && $_GET[$param_name] === $val;
}

@n8kowald
Copy link
Author

n8kowald commented Mar 21, 2022

There should be a tailing ; after the last add_filter() call, when we are not using a closing ?> tag: https://stackoverflow.com/a/29284131/13765033

Thanks David. I've updated the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment