How can I generalise this PHP function for conditional custom attributes?
-
I’m not a PHP programmer (but have been a programmer by profession for many years – just don’t know PHP)!
How can I generalise the following bit of PHP and HTML that I found on the forum:
In single event format{has_att_website} <p> <strong>Website</strong>; <a href="https://#_ATT{Website}" target="_blank" rel="noopener noreferrer">#_ATT{Website}</a> </p> {/has_att_website}
PHP added to make the output conditional on the custom attribute ~_ATT{Website} not being empty:
/** * add some conditional output conditions for Events Manager * @param string $replacement * @param string $condition * @param string $match * @param object $EM_Event * @return string */ function filterEventOutputCondition($replacement, $condition, $match, $EM_Event){ if (is_object($EM_Event)) { switch ($condition) { // replace LF with HTML line breaks case 'nl2br': // remove conditional $replacement = preg_replace('/\{\/?nl2br\}/', '', $match); // process any placeholders and replace LF $replacement = nl2br($EM_Event->output($replacement)); break; // #_ATT{Website} case 'has_att_website': if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['Website'])) $replacement = preg_replace('/\{\/?has_att_website\}/', '', $match); else $replacement = ''; break; } } return $replacement; } add_filter('em_event_output_condition', 'filterEventOutputCondition', 10, 4);
So that I can use it on any custom attribute….
How do I add an extra parameter, being the custom attribute name, both to how I write the code on the single event format and to the PHP so that I can have the following on the single event format:
[NOTE – I have left out the ‘parameter’ of the custom attribute name, since I don’t know where it would go]
This would output price for subscribers [#_ATT{MemberPrice}], non-subscribers [#_ATT{NonMemberPrice}] or single price [#_ATT{SinglePrice}], based on which attributes had values.{process_conditional_attribute} <p> <strong>Prix adherent(e):</strong> #_ATT{MemberPrice} </p> {/process_conditional_attribute}? {process_conditional_attribute}? <p> <strong>Prix non-adherent(e):</strong> #_ATT{NonMemberPrice} </p> {/process_conditional_attribute}? {process_conditional_attribute} <p> <strong>Prix:</strong> #_ATT{SinglePrice} </p> {process_conditional_attribute}
I’d also use it for link to external booking site if there was a value in the custom attribute used for the internal reference on the booking site, and to not display the link if there was no value.
Can anyone help me with this?
In the meantime I’ll learn about child themes so I can put it somewhere safe so it won’t get overwritten by any updates!Thanks in advance. ??
- This topic was modified 4 years, 8 months ago by . Reason: corrected problem with HTML by tagging as code
- This topic was modified 4 years, 8 months ago by . Reason: Put back and
The page I need help with: [log in to see the link]
- The topic ‘How can I generalise this PHP function for conditional custom attributes?’ is closed to new replies.