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

Add and Remove Profile Fields to WordPress User?Fo

WBOY
Freigeben: 2016-06-06 20:08:57
Original
1067 Leute haben es durchsucht

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');
Nach dem Login kopieren

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');
Nach dem Login kopieren

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');
Nach dem Login kopieren

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');
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage