elegantthemes / Divi-Beta

8 stars 0 forks source link

How To Filter The Output Of Exisiting Divi 5 Module #70

Closed UmerCheema-WPD closed 1 month ago

UmerCheema-WPD commented 4 months ago

If we want to filter the output of the contact form module, we can achieve this in Divi 4 using add_filter('et_module_shortcode_output', 'my_callback_function').

How can we achieve this in Divi 5.0? Is there any alternate hook for this?

Hook

apply_filters( 'et_module_shortcode_output', $output, $render_slug, $this )

Code Example

 /**
 * Filters the output of the Divi contact form module.
 * This function modifies the output of the contact form module when certain conditions are met,
 * adding a custom paragraph at the top of the form.
 *
 * @param string $output The current module output.
 * @param string $render_slug The slug of the module being rendered.
 * @param object $module The module object.
 *
 * @return string The modified or original module output.
 */
if (!function_exists('filter_et_pb_contact_form_output')) :
    function filter_et_pb_contact_form_output($output, $render_slug, $module)
    {
        // Return if Frontend Builder is enabled
        if (function_exists('et_fb_is_enabled') && et_fb_is_enabled()) {
            return $output;
        }
        // Return if Backend Builder is enabled
        if (function_exists('et_builder_bfb_enabled') && et_builder_bfb_enabled()) {
            return $output;
        }
        // Return if in admin, doing AJAX, output is array, or output is empty
        if (is_admin() || wp_doing_ajax() || is_array($output) || empty($output)) {
            return $output;
        }
        // Return if the render slug does not match the contact form module
        if ('et_pb_contact_form' !== $render_slug) {
            return $output;
        }
        // Check if the module's unique ID matches the specified ID
        if (isset($module->props['_unique_id']) && 'xxxxxxxx' === $module->props['_unique_id']) {
            // Load the output into a DOMDocument object
            $dom = new DOMDocument;
            libxml_use_internal_errors(true); // Suppress warnings due to malformed HTML
            $dom->loadHTML($output, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
            libxml_clear_errors();
            // Create a new paragraph element with custom text and styling
            $newParagraph = $dom->createElement('p', 'This is a custom paragraph added at the top of the form.');
            $newParagraph->setAttribute('style', 'color: red; font-size: 18px;font-weight:800; text-align: center;');
            // Insert the new paragraph at the top of the form
            $form = $dom->getElementsByTagName('form')->item(0);
            if ($form) {
                $form->insertBefore($newParagraph, $form->firstChild);
            }

            // Return the modified HTML
            return $dom->saveHTML();
        }

        // Return the original output if no conditions are met
        return $output;
    }

    add_filter('et_module_shortcode_output', 'filter_et_pb_contact_form_output', 10, 3);
endif;

Screenshot

Filter Ouput

robicse11127 commented 1 month ago

Closing this issue as per the questioner confirmation. Discord thread :: https://discord.com/channels/1041765492907589683/1261273317810372630