WP_Widget::get_field_name( string $field_name )

Constructs name attributes for use in form() fields


Description Description

This function should be used in form() methods to create name attributes for fields to be saved by update()


Top ↑

Parameters Parameters

$field_name

(string) (Required) Field name


Top ↑

Return Return

(string) Name attribute for $field_name


Top ↑

Source Source

File: wp-includes/class-wp-widget.php

	public function get_field_name( $field_name ) {
		$pos = strpos( $field_name, '[' );
		if ( false === $pos ) {
			return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
		} else {
			return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
		}
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Array format field names are now accepted.
2.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    function wpdocs_widget_form( $instance ) {
    	?>
    	<p>
    		<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">Title:</label>
    		<input type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>">
    	</p>
    	<?php
    }	
    

You must log in before being able to contribute a note or feedback.