• An empty name attribute on a form element is considered invalid markup (at least by the w3 validator).

    By looking at the source code I’ve found that the attribute can be controlled using the html_name attribute on the shorttag used to insert the form – but I can’t find this documented anywhere.

    It seems like a bug that it defaults to empty…?

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Mikk3lRo

    (@mikk3lro)

    This question on stack overflow is what prompted me to check up on it by the way:
    https://stackoverflow.com/questions/31306391/wordpress-custom-form-7-missing-form-name/31310224

    Thread Starter Mikk3lRo

    (@mikk3lro)

    It appears to have happened between version 3.9 and 3.9.1

    Around line 212-215 (depending on version) in includes/functions.php inside the function wpcf7_format_attrs an empty check has been dropped:

    if ( ” !== $value ) {
    $html .= sprintf( ‘ %s=”%s”‘, $att, esc_attr( $value ) );
    }

    Is now simply:

    $html .= sprintf( ‘ %s=”%s”‘, $att, esc_attr( $value ) );

    hi,
    I use your version 4.2.1. this is code you mention in includes/functions.php.

    function wpcf7_format_atts( $atts ) {
    	$html = '';
    
    	$prioritized_atts = array( 'type', 'name', 'value' );
    
    	foreach ( $prioritized_atts as $att ) {
    		if ( isset( $atts[$att] ) ) {
    			$value = trim( $atts[$att] );
    			$html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
    			unset( $atts[$att] );
    		}
    	}
    
    	foreach ( $atts as $key => $value ) {
    		$key = strtolower( trim( $key ) );
    
    		if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $key ) ) {
    			continue;
    		}
    
    		$value = trim( $value );
    
    		if ( '' !== $value ) {
    			$html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) );
    		}
    	}
    
    	$html = trim( $html );
    
    	return $html;
    }

    But it still has error.

    Thread Starter Mikk3lRo

    (@mikk3lro)

    Yes, that’s exactly the point – the first foreach block has no empty-check:

    if ( isset( $atts[$att] ) ) {
    	$value = trim( $atts[$att] );
    	$html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
    	unset( $atts[$att] );
    }

    should be:

    if ( isset( $atts[$att] ) ) {
    	$value = trim( $atts[$att] );
    	if ($value !== '') {
    		$html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
    	}
    	unset( $atts[$att] );
    }

    …well actually I can’t say what it should be – but that would take care of the empty name attribute ??

    Great find! Is there a way to include this fix without losing it in the next update? Any more word on the developer re-adding?

    I gave a try and it doesn’t seem to solve the issue in 4.4.2 – did the update break this solution?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Bug? Form name attribute is empty by default’ is closed to new replies.