> php教程 > php手册 > Add and Remove Profile Fields to WordPress User?Fo

Add and Remove Profile Fields to WordPress User?Fo

WBOY
풀어 주다: 2016-06-06 20:08:57
원래의
1067명이 탐색했습니다.

The WordPress user profile screen allows you to set values for social services but some default services are irrelevant, namely AIM and Yahoo! IM; ?add to that the fact that Twitter and Facebook fields are missing. ?You quickly realize tha

The WordPress user profile screen allows you to set values for social services but some default services are irrelevant, namely AIM and Yahoo! IM; ?add to that the fact that Twitter and Facebook fields are missing. ?You quickly realize that the default form…needs work. ?WordPress provides a method for adding and removing profile fields. ?Let me show you how it works!

Filter Setup

The first step is creating a function in your functions.php file which will accept an array of profile keys and values:

function modify_contact_methods($profile_fields) {
	// Field addition and removal will be done here
}
add_filter('user_contactmethods', 'modify_contact_methods');
로그인 후 복사

This function provides access to that important protected array. ?The returned value becomes the list of user profile fields.

Adding a Profile Field

Adding a new field, Twitter handle for example, includes adding a key to the passed in array, with a value which will act as the field label:

function modify_contact_methods($profile_fields) {
	// Add new fields
	$profile_fields['twitter'] = 'Twitter Username';
	$profile_fields['facebook'] = 'Facebook URL';
	$profile_fields['gplus'] = 'Google+ URL';
	return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');
로그인 후 복사

Simply adding that key/value to the array adds a new field to the form.

Removing a Profile Field

Conversely, removing a key from said array removes a field from the user profile form:

function modify_contact_methods($profile_fields) {
	// Add new fields
	$profile_fields['twitter'] = 'Twitter Username';
	$profile_fields['facebook'] = 'Facebook URL';
	$profile_fields['gplus'] = 'Google+ URL';
	// Remove old fields
	unset($profile_fields['aim']);
	return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');
로그인 후 복사

The code above removes the AIM field from the edit profile form.

Retrieving Custom Field Values

To retrieve custom field values, use the?get_the_author_meta method:

// Retrieve a custom field value
$twitterHandle = get_the_author_meta('twitter');
로그인 후 복사

The ability to easily add profile form fields is awesome; ?super easy to do, no plugin required!

Read the full article at: Add and Remove Profile Fields to WordPress User Form

Treehouse

Sencha Conference

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿