wordpress 字段
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!
WordPress用户配置文件屏幕允许您设置社交服务的值,但是一些不相关的默认服务,即AIM和Yahoo!。 IM; 此外,Twitter和Facebook字段缺失。 您很快意识到默认格式...需要工作。 WordPress提供了一种添加和删除配置文件字段的方法。 让我告诉你它是如何工作的!
过滤器设置 (Filter Setup)
The first step is creating a function in your functions.php file which will accept an array of profile keys and values:
第一步是在functions.php文件中创建一个函数,该函数将接受一组配置文件键和值:
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:
添加一个新字段,例如Twitter句柄,包括向传递的数组添加一个键,该键的值将用作字段标签:
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.
上面的代码从编辑配置文件表单中删除了AIM字段。
检索自定义字段值 (Retrieving Custom Field Values)
To retrieve custom field values, use the get_the_author_meta method:
要检索自定义字段值,请使用get_the_author_meta方法:
// 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!
轻松添加配置文件表单字段的功能非常强大; 超级容易做,不需要插件!
wordpress 字段