[Resolved] Shortcodes for CPF in Conditional not working Divi Theme Builder
This thread is resolved. Here is a description of the problem and solution.
Problem:
The customer was attempting to use a shortcode to display a custom field (CPF) conditionally within the Divi Theme Builder’s tab module. While the shortcode functioned correctly outside the Theme Builder, it only displayed the raw shortcode within the builder, failing to render the expected content. The issue appeared to stem from Divi’s Theme Builder potentially overwriting post variables required by shortcodes.
Solution:
A workaround was suggested due to the limitations in Divi’s handling of shortcodes within the Theme Builder. A custom PHP function was created and added to the theme's functions.php file to handle the conditional logic and output the custom field directly:
function display_reisekosten_func() {
if (get_post_meta(get_the_ID(), 'wpcf-reisekosten-pauschal-individuell', true) == '1') {
return '<h2>Reisekosten</h2>' . do_shortcode('[types field="reisekosten-min-km"][/types]');
}
}
add_shortcode('display_reisekosten', 'display_reisekosten_func');
The shortcode [display_reisekosten] could then be used within a Divi Code Module to render the content conditionally. While not a guaranteed solution, this approach provided a viable alternative until Divi resolves the underlying issue.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Tell us what you are trying to do?
I am using Divi Theme Builder Template for a site and try to use a shortcode to display a cpf inside of a conditional. The Shortcode is inside of a tab-module.
Here is an example code:
The condition is working fine. Problem is that the shortcode is not showing the content, it just shows the shortcode.
If I use the shortcode on a site in a tab-module (and not on the divi builder) everything is working! The shortcode does what I expected.
Hello. Thank you for contacting the Toolset support.
Can you please share details on what Divi template you added conditional statement and what conditional statement is working and where exactly on what part of the Divi template you would like to see it working.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Thank you for replying. I contacted the Divi support, they were able to replicate the issue. They told me, that they need complex adjustments within the Theme Builder to fix it.
Unfortunately the are not able to give me a solution how to handle this problem. Maybe you as a toolset expert know a solution or another workaround to get it working?
If not, no worries.
Thanks beforehand.
Thanks for following up. Given the limitation within Divi's Theme Builder, there’s one workaround you could try, though I can’t guarantee it will fully resolve the issue given the challenges Divi has with shortcode processing. Here’s an approach that may provide a workaround:
You could add a custom function to your theme’s functions.php file, which handles the conditional logic and outputs the custom field. Here’s an example:
function display_reisekosten_func() {
if (get_post_meta(get_the_ID(), 'wpcf-reisekosten-pauschal-individuell', true) == '1') {
return '<h2>Reisekosten</h2>' . do_shortcode('[types field="reisekosten-min-km"][/types]');
}
}
add_shortcode('display_reisekosten', 'display_reisekosten_func');
After adding this to your theme, you can use [display_reisekosten] in a Divi Code Module within your template to attempt to display the conditional content.
Important Disclaimer
Since the issue stems from Divi’s handling of shortcodes in the Theme Builder, there’s a chance this workaround may not work consistently. If it doesn’t work, I’m afraid a more robust solution would depend on Divi making adjustments on their end.
thank you for your help. I wish the divi support would also be so solution-oriented.
My code is a bit more complex than this example code but it's working.
I think it's the best solution . Hopefully the divi team is going to fix their bug.