webdevstudios / cmb2-user-select
一个特殊的CMB2字段,允许用户定义用户自动完成文本字段
v0.2.3
2017-06-07 15:30 UTC
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2024-09-14 19:09:22 UTC
README
一个特殊的CMB2字段,允许用户定义用户自动完成文本字段
示例
$cmb2->add_field( array( 'name' => 'Author',, 'id' => 'author', 'desc' => 'Type the name of the author and select from the options', 'type' => 'user_select_text' 'options' => array( 'user_roles' => array( 'role1', 'role2' ), // Specify which roles to query for. ), ) );
返回的值是一个包含用户 id
和 name
的数组,例如
array( 'id' => 1 'name' => 'Joe Blogs' )
这将直接保存到数据库时进行序列化。
如果您只想存储用户的ID,可以使用 escape_cb
和 sanitization_cb
来转换数据。
/** * Takes the id from the database and returns an array for user_select_text * @param int $value * @return array('name' => string, 'id' => int) */ function id_to_user_select_text($value) { $user = get_user_by('id', (int)$value); return array( 'name' => $user->display_name, 'id' => $user->ID, ); } /** * Takes the array from user_select_text and returns the id for the database * @param array('name' => string, 'id' => int) $value * @return int */ function user_select_text_to_id($value) { return $value['id']; } $cmb2->add_field( array( // ...snip... 'escape_cb' => 'user_select_text_to_id', 'sanitization_cb' => 'id_to_user_select_text', ) );