我使用以下代码添加自定义元字段并在管理员用户个人资料中显示它们。
print(("\nsets to %02d:%02d:%02d\n" % (hour, minute, second)))
我现在想要的是显示每个自定义字段的不同描述,而不是
* Register Custom Fields in User Profiles.
* First we need to set-up which meta fields are important, it was chosen to go for a modular set-up with an array. This way the method is flexible and easy for large number of fields. The key element to the array entry is the meta field name, whereas the value gives a nice printable name.
*/
function mysite_custom_define() {
$mysite_custom_meta_fields = array();
$mysite_custom_meta_fields['facebook'] = 'Facebook Profile';
$mysite_custom_meta_fields['instagram'] = 'Instagram Profile';
$mysite_custom_meta_fields['twitter'] = 'Twitter Handle';
return $mysite_custom_meta_fields;
}
/**
* Register Custom Fields in User Profiles.
* Create the Columns.
*/
function mysite_columns($defaults) {
$meta_number = 0;
$mysite_custom_meta_fields = mysite_custom_define();
foreach ($mysite_custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
$defaults[('mysite-usercolumn-' . $meta_number . '')] = __($meta_disp_name, 'user-column');
}
return $defaults;
}
/**
* Register Custom Fields in User Profiles.
* Fill the Columns.
*/
function mysite_custom_columns($value, $column_name, $id) {
$meta_number = 0;
$mysite_custom_meta_fields = mysite_custom_define();
foreach ($mysite_custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
if( $column_name == ('mysite-usercolumn-' . $meta_number . '') ) {
return get_the_author_meta($meta_field_name, $id );
}
}
}
/**
* Register Custom Fields in User Profiles.
* Show the information on the user profile and edit pages.
*/
function mysite_show_extra_profile_fields($user) {
print('<h2>Social Profiles Information</h2>');
print('<table class="form-table">');
$meta_number = 0;
$mysite_custom_meta_fields = mysite_custom_define();
foreach ($mysite_custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
print('<tr>');
print('<th><label for="' . $meta_field_name . '">' . $meta_disp_name . '</label></th>');
print('<td>');
print('<input type="url" name="' . $meta_field_name . '" id="' . $meta_field_name . '" value="' . esc_attr( get_the_author_meta($meta_field_name, $user->ID ) ) . '" class="regular-text code" /><br />');
print('<p class="description">Fill in the complete URL of your ' . $meta_disp_name . '</p>');
print('</td>');
print('</tr>');
}
print('</table>');
}
/**
* Register Custom Fields in User Profiles.
* Save Changes.
*/
function mysite_save_extra_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id))
return false;
$meta_number = 0;
$mysite_custom_meta_fields = mysite_custom_define();
foreach ($mysite_custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
}
}
/**
* Register Custom Fields in User Profiles.
* Integrating the Functions in wordpress.
*/
add_action('show_user_profile', 'mysite_show_extra_profile_fields');
add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
add_action('personal_options_update', 'mysite_save_extra_profile_fields');
add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
add_filter('manage_users_columns', 'mysite_columns', 15, 1); ```
我希望你明白我的要求。我想在每个元字段后显示描述,并希望从数组中传递描述。任何帮助将不胜感激
答案 0 :(得分:0)
这样的事,也许吧? (使用此代替当前的mysite_show_extra_profile_fields()
)
<?php
function mysite_show_extra_profile_fields($user) {
print('<h2>Social Profiles Information</h2>');
print('<table class="form-table">');
$meta_number = 0;
$mysite_custom_meta_fields = mysite_custom_define();
// Match keys as defined in mysite_custom_define().
$custom_meta_fields_desc = array(
// Format: '{KEY}' => '{DESCRIPTION}',
'facebook' => 'Description for Facebook',
'instagram' => 'Description for Instagram',
'twitter' => 'Description for Twitter',
);
foreach ($mysite_custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
print('<tr>');
print('<th><label for="' . $meta_field_name . '">' . $meta_disp_name . '</label></th>');
print('<td>');
print('<input type="url" name="' . $meta_field_name . '" id="' . $meta_field_name . '" value="' . esc_attr( get_the_author_meta($meta_field_name, $user->ID ) ) . '" class="regular-text code" /><br />');
$desc = isset( $custom_meta_fields_desc[ $meta_field_name ] ) ?
$custom_meta_fields_desc[ $meta_field_name ] : // Use custom description.
'Fill in the complete URL of your ' . $meta_disp_name; // Use default description.
print('<p class="description">' . $desc . '</p>');
print('</td>');
print('</tr>');
}
print('</table>');
}