Skip to content

Readonly Option for Contact Form 7 WP Plugin

Contact Form 7 is great. As it says: "simple". This is to add up readonly option to that simplicity.

E.g. for text inputs:

/wp-content/plugins/contact-form-7/modules/text.php

Code reads:

        $atts = '';
        $id_att = '';
        $class_att = '';
        $size_att = '';
        $maxlength_att = '';

        if ( 'email' == $type || 'email*' == $type )
                $class_att .= ' wpcf7-validates-as-email';

        if ( 'text*' == $type || 'email*' == $type )
                $class_att .= ' wpcf7-validates-as-required';

        foreach ( $options as $option ) {
                if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
                        $id_att = $matches[1];

                } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
                        $class_att .= ' ' . $matches[1];

                } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
                        $size_att = (int) $matches[1];
                        $maxlength_att = (int) $matches[2];
                }
        }

        if ( $id_att )
                $atts .= ' id="' . trim( $id_att ) . '"';

        if ( $class_att )
                $atts .= ' class="' . trim( $class_att ) . '"';

        if ( $size_att )
                $atts .= ' size="' . $size_att . '"';
        else
                $atts .= ' size="40"'; // default size

        if ( $maxlength_att )
                $atts .= ' maxlength="' . $maxlength_att . '"';

Change it to:

        $atts = '';
        $id_att = '';
        $class_att = '';
        $readonly_att = false;
        $size_att = '';
        $maxlength_att = '';

        if ( 'email' == $type || 'email*' == $type )
                $class_att .= ' wpcf7-validates-as-email';

        if ( 'text*' == $type || 'email*' == $type )
                $class_att .= ' wpcf7-validates-as-required';

        foreach ( $options as $option ) {
                if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
                        $id_att = $matches[1];

                } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
                        $class_att .= ' ' . $matches[1];

                } elseif ( preg_match( '%^readonly$%', $option, $matches ) ) {
                        $readonly_att = true;

                } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
                        $size_att = (int) $matches[1];
                        $maxlength_att = (int) $matches[2];
                }
        }

        if ( $id_att )
                $atts .= ' id="' . trim( $id_att ) . '"';

        if ( $class_att )
                $atts .= ' class="' . trim( $class_att ) . '"';

        if ( $readonly_att )
                $atts .= ' readonly="readonly"';

        if ( $size_att )
                $atts .= ' size="' . $size_att . '"';
        else
                $atts .= ' size="40"'; // default size

        if ( $maxlength_att )
                $atts .= ' maxlength="' . $maxlength_att . '"';

You can now use readonly attribute. E.g. [text* field_name readonly] produces an input field that has readonly="readonly" definition in it.

One Comment

  1. verogabri wrote:

    Thanks.

    I was in trouble with a textarea with some privacy note,
    I colud do the right job with your tutorial

    Gabri ^_^

    Posted on 29-May-12 at 4:58 pm | Permalink