Enable Shortcodes In Divi Fields [Contact Form etc]

There are several fields in the Divi Theme’s various modules where you can insert text and URLs to be displayed by the modules. But frequently, these are restricted to the static text you enter in the settings of the modules. What would happen if you wanted to be able to use shortcodes to dynamically generate the URLs or text, such a URL gathered using the Advanced Custom Fields (ACF) plugin? This is the procedure.

I created a shortcode to get IP address from contact form (to block spam senders ASN) it was showing as text, after added this codes to functions.php and added the field of custom_messaage i could execute the shortcode in here.

Enable Shortcodes via functions.php for Divi Modules

The following PHP code (taken from my Divi Shortcode Enabler plugin) will search the page / post content for various module fields, and process any shortcodes contained within them.

if (!class_exists('DBDSE_EnableShortcodesInModuleFields')) {
class DBDSE_EnableShortcodesInModuleFields {

static public function supportedFields() {
return apply_filters(
'dbdsp_fields_to_process', 
array(
'et_pb_accordion_item' => array('title'),
'et_pb_blurb' => array('title', 'url', 'image', 'alt'),
'et_pb_button' => array('button_url', 'button_text'),
'et_pb_circle_counter' => array('title', 'number'),
'et_pb_cta' => array('title', 'button_text', 'button_url'),
'et_pb_image' => array('url', 'src', 'title_text', 'alt'),
'et_pb_number_counter' => array('title', 'number'),
'et_pb_counter' => array('percent'), 
'et_pb_pricing_table' => array('title', 'subtitle', 'currency', 'sum', 'button_text', 'button_url'),
'et_pb_tab' => array('title'),
'et_pb_slide' => array('heading', 'button_text', 'button_link', 'image_alt', 'title_text'),
'db_pb_slide' => array('button_text_2', 'button_link_2'), // Divi Booster added second slide buttons
'et_pb_fullwidth_header' => array('title', 'subhead', 'button_one_text', 'button_two_text', 'button_one_url', 'button_two_url'),
'et_pb_fullwidth_image' => array('src', 'title_text', 'alt'),
'et_pb_contact_field' => array('field_title', 'custom_message')
)
);
}

public function init() {
add_filter('the_content', array($this, 'processShortcodes')); // Standard content
add_filter('et_builder_render_layout', array($this, 'processShortcodes')); // Theme builder layout content
add_filter('dbdse_et_pb_layout_content', array($this, 'processShortcodes')); // Global module content
add_filter('et_pb_module_shortcode_attributes', array($this, 'preventShortcodeEncodingInModuleSettings'), 11, 3);
}

public function processShortcodes($content) {
$modules = self::supportedFields();
do_action('dbdsp_pre_shortcode_processing');
foreach((array) self::supportedFields() as $module=>$fields) {
foreach($fields as $field) {
$regex = '#['.preg_quote($module).' [^]]*?\b'.preg_quote($field).'="([^"]+)"#';
$content = preg_replace_callback($regex, array($this, 'processMatchedAttribute'), $content);
}
}
do_action('dbdsp_post_shortcode_processing');
return $content;
}

protected function processMatchedAttribute($matches) {

// Exit if not properly matched
if (!is_array($matches) || !isset($matches[0])) { return ''; } 
if (!isset($matches[1])) { return $matches[0]; }

// Define character replacements
$encoded = array('%'.'22', '%'.'91', '%'.'93');
$decoded = array('"', '[', ']');

// Get the decoded parameter value
$val = $matches[1];
$val = str_replace($encoded, $decoded, $val); // decode encoded characters
$val = do_shortcode($val);
$val = str_replace($decoded, $encoded, $val); // re-encode

// Return the replacement value
$result = str_replace($matches[1], $val, $matches[0]);

return $result;
}

public function preventShortcodeEncodingInModuleSettings($props, $attrs, $render_slug) {
if (!is_array($props)) { return $props; }
if (!empty($_GET['et_fb'])) {
if ($render_slug === 'et_pb_image' && !empty($attrs['url']) && strpos($attrs['url'], '[') !== false && strpos($attrs['url'], ']') !== false) { 
$props['url'] = $attrs['url'];
$props['url'] = str_replace(array('[', ']', '"'), array('[', ']', '"'), $props['url']);
}
}
return $props; 
}
}
(new DBDSE_EnableShortcodesInModuleFields)->init();
}

Enabled Shortcodes for This Fields

    • Accordion Module
      • Title
    • Bar Counter Module
      • Percent
    • Blurb Module
      • Title
      • Image
      • URL
      • Alt
    • Button Module
      • Button Text
      • Button URL
    • Call To Action (CTA) Module
      • Title
      • Button Text
      • Button URL
    • Circle Counter Module
      • Title
      • Number
    • Columns
      • Column Link URL
    • Contact Form Module
      • Email
      • Redirect URL
      • Message Pattern
      • Field Titles (Placeholder)
    • Fullwidth Header Module
      • Title
      • Subheading Text
      • Button #1 Link Text
      • Button #2 Link Text
      • Button #1 Link URL
      • Button #2 Link URL
    • Fullwidth Image Module
      • Image URL
      • Title Text
      • Alt
  • Image Module
    • Image URL
    • Link URL
    • Title Text
    • Alt
  • Number Counter Module
    • Title
    • Number
  • Pricing Table Module
    • Title
    • Subtitle
    • Currency
    • Sum
    • Button Text
    • Button URL
  • Rows
    • Row Link URL
  • Slider Module Slides
    • Title
    • Button Text
    • Button URL
    • Title Text
    • Alt
  • Social Media Follow Module
    • Account Link URL
  • Tab Module
    • Title
  • All Modules, Rows and Sections
    • Background Image URL
    • Module Link URL
    • CSS ID
    • CSS Class

https://divibooster.com/allowing-shortcodes-in-divi-urls-buttons-images-etc/

Related Posts