Last active
March 21, 2022 21:57
-
-
Save n8kowald/1ccf396f47ba6e6fc9b16e5e7063ed00 to your computer and use it in GitHub Desktop.
If elseif else shortcode example
This file contains 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 | |
// 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] | |
*/ |
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;
}
There should be a tailing
;
after the lastadd_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
There should be a tailing
;
after the lastadd_filter()
call, when we are not using a closing?>
tag: https://stackoverflow.com/a/29284131/13765033