繁体   English   中英

如何在 PHP 中添加多个过滤器

[英]How to Add Multiple Filters in PHP

我有这段代码可以将具有特定 CSS 类的输入字段转换为只读字段。 代码运行良好,但是在不再次复制和粘贴整个代码的情况下将第二个表单添加到过滤器中的最佳做法是什么?

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

您不必复制所有代码。 我推断这段代码似乎来自 WordPress 中的某个地方,带有 Google 表单。

您唯一需要做的就是将过滤器也应用于其他表单。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM