How to Add Multiple Filters in PHP

Issue

I have this code that converts input fields with a specific CSS class into a read-only field. The code is working well but what’s the best practice to add a second form into the filter without copying and pasting the entire code again?

// ID of the Form
add_filter( 'gform_pre_render_138', 'add_readonly_script' );
function add_readonly_script( $form ) {
    ?>
    <script type="text/javascript">
        jQuery(document).on('gform_post_render', function(){

            /* apply only to a input with a class of gf_readonly */
            jQuery(".gf_readonly input").attr("readonly","readonly");
        });
    </script>
    <?php
    return $form;
}

Solution

You don’t have to duplicate all the code. I´m inferring that this piece of code come from somewhere in WordPress with Google Forms seems.

The only thing you have to do is apply filter to the other form too.

add_filter( 'gform_pre_render_my_second_form_id', 'add_readonly_script' );
add_filter( 'gform_pre_render_138', 'add_readonly_script' );
function add_readonly_script( $form ) {
    ?>
    <script type="text/javascript">
        jQuery(document).on('gform_post_render', function(){

            /* apply only to a input with a class of gf_readonly */
            jQuery(".gf_readonly input").attr("readonly","readonly");
        });
    </script>
    <?php
    return $form;
}

Answered By – Manuel Glez

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published