Home > OS >  How to Add Multiple Filters in PHP
How to Add Multiple Filters in PHP

Time:06-14

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;
}

CodePudding user response:

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;
}
  • Related