WP_Widget::get_field_name() WordPress Method

The WP_Widget::get_field_name() method is used to retrieve the name attribute for a given field id. This is useful for creating form fields that have multiple values.

WP_Widget::get_field_name( string $field_name ) #

Constructs name attributes for use in form() fields


Description

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


Top ↑

Parameters

$field_name

(string)(Required)Field name.


Top ↑

Return

(string) Name attribute for $field_name.


Top ↑

Source

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

	public function get_field_name( $field_name ) {
		$pos = strpos( $field_name, '[' );

		if ( false !== $pos ) {
			// Replace the first occurrence of '[' with ']['.
			$field_name = '[' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
		} else {
			$field_name = '[' . $field_name . ']';
		}

		return 'widget-' . $this->id_base . '[' . $this->number . ']' . $field_name;
	}

Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.