User Meta containers向用户编辑屏幕添加额外的字段。 每个用户的字段数据作为用户元单独存储(请参阅 add_user_meta)。 请注意,默认情况下,只有具有 manage_options
功能的用户才能访问此容器。 要覆盖此行为,请参阅 Hooks 部分中的 carbon_fields_user_meta_container_admin_only_access
过滤器。
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'user_meta', 'Address' )
->add_fields( array(
Field::make( 'text', 'crb_city_and_post', 'City and post code' ),
Field::make( 'text', 'crb_street', 'Street Name' ),
) );
访问字段值
要访问字段值,您需要使用函数 carbon_get_user_meta( $user_d, $name )
,其中:
参数 | 描述 |
---|---|
$user_id |
输入值的用户 ID。 |
$name |
要检索的字段的字段名称模式。 |
<!-- Simple field -->
<p>Author address: <?php echo carbon_get_user_meta( get_the_author_meta( 'ID' ), 'crb_street' ); ?></p>
<!-- Complex field -->
<?php
$phone_numbers = carbon_get_user_meta( get_the_author_meta( 'ID' ), 'crb_phone_numbers' );
foreach ( $phone_numbers as $phone ) {
echo $phone['country_code'] . '-' . $phone['number'];
}
?>
保存后,会调用钩子,它允许您在保存后钩子附加功能。 它接受参数,即被更新的用户的 ID。carbon_fields_user_meta_container_saved
$user_id