有没有办法使自定义字段仅供管理员编辑?

时间:2012-08-13 作者:lowestofthekeys

我发现以下代码将自定义字段放置到用户的配置文件中:

<?php   
function extra_user_profile_fields( $user ) {
?>
<h3><?php _e("Roof Asset Information", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="facility"><?php _e("Facility"); ?></label></th>
<td>
<input type="text" name="facility" id="facility" value="<?php echo esc_attr(        
get_the_author_meta( \'facility\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter the facility name."); ?></span>
</td>
</tr>
<tr>
<th><label for="roofsection"><?php _e("Roof Section"); ?></label></th>
<td>
<input type="text" name="roofsection" id="roofsection" value="<?php echo esc_attr(     
get_the_author_meta( \'roofsection\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter the roof section."); ?></span>
</td>
</tr>
<tr>
<th><label for="footage"><?php _e("Sq. Feet"); ?></label></th>
<td>
<input type="text" name="footage" id="footage" value="<?php echo esc_attr(    
get_the_author_meta( \'footage\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter the square footage."); ?></span>
</td>
</tr>

</table>
<?php }

add_action( \'personal_options_update\', \'save_extra_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'save_extra_user_profile_fields\' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }

update_user_meta( $user_id, \'facility\', $_POST[\'facility\'] );
update_user_meta( $user_id, \'roofsection\', $_POST[\'roofsection\'] );
update_user_meta( $user_id, \'footage\', $_POST[\'footage\'] );

}    
是否有办法向该代码添加额外功能,以便只有管理员才能编辑自定义字段?

1 个回复
SO网友:Martin

<input type="text" name="facility" id="facility" value="<?php echo esc_attr(        
get_the_author_meta( \'facility\', $user->ID ) ); ?>"  
<?php if (!is_admin()) { echo \'disabled="disabled"\'; }
class="regular-text" /><br />
<span class="description"><?php _e("Please enter the facility name."); ?></span>
将第三行添加到您不希望非管理员编辑的所有输入中。

结束

相关推荐

使用GET_USERS获取多个角色

我有一些这样的代码: $query_args = array(); $query_args[\'fields\'] = array( \'ID\', \'display_name\' ); $query_args[\'role\'] = \'subscriber\'; $users = get_users( $query_args ); foreach ($users as $user) $users_array[$user->ID] = $user->