Created
November 26, 2023 00:26
-
-
Save CamiloGarciaLaRotta/05a9c6e3f67ee21e3f8cf14bbbbf0598 to your computer and use it in GitHub Desktop.
Snippets to add conditional rendering for custom attrs: {has_foo}print foo{/has_foo}
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
// see https://wp-events-plugin.com/documentation/conditional-placeholders/ | |
// for the conditional funcs that we have out of the box | |
// see https://wp-events-plugin.com/tutorials/creating-conditional-placeholders-for-events/ | |
// for how to introduce conditional funcs for custom attributes | |
// the docs explain what each each variable means | |
function em_event_output_condition_filter($replacement, $condition, $match, $EM_Event){ | |
// Checks for has_FOO conditional | |
if(is_object($EM_Event) && preg_match('/^has_(FOO)$/', $condition, $matches)){ | |
if(array_key_exists($matches[1], $EM_Event->event_attributes) && !empty($EM_Event->event_attributes[$matches[1]]) ){ | |
$replacement = preg_replace("/\{\/?$condition\}/", '', $match); | |
}else{ | |
$replacement = ''; | |
} | |
} | |
// Checks for no_FOO conditional | |
if(is_object($EM_Event) && preg_match('/^no_(FOO)$/', $condition, $matches)){ | |
if(!array_key_exists($matches[1], $EM_Event->event_attributes) || empty($EM_Event->event_attributes[$matches[1]]) ){ | |
$replacement = preg_replace("/\{\/?$condition\}/", '', $match); | |
}else{ | |
$replacement = ''; | |
} | |
} | |
//Return the result | |
return $replacement; | |
} | |
// this is super weird syntax, but it what registers the func that will parse has_foo/no_foo | |
add_filter('em_event_output_condition', 'em_event_output_condition_filter', 1, 4); | |
// Here is another example: https://pastebin.com/4amjiMwi | |
// Here's where we would paste the PHP snippet – http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment