Hey @martnick,
Interesting use case! You can approach this in two ways:
1. Make the field names dynamic by adding the user id as a suffix:
$user_id = get_current_user_id();
Field::make( 'checkbox', 'crb_some_option_' . $user_id, __( 'Some Option', 'crb' ) ),
You can also make a function that builds the meta names for you just for convenience.
2. Modify the container datastore (not really recommended, but a possible solution):
use Carbon_Fields\Container\Container;
use Carbon_Fields\Field\Field;
use Carbon_Fields\Datastore\User_Meta_Datastore;
$users_datastore = new User_Meta_Datastore();
$users_datastore->set_id( get_current_user_id() );
$theme_options = Container::make( 'theme_options', __( 'Theme Options', 'crb' ) );
$theme_options->set_datastore( $users_datastore );
$theme_options
->add_fields( array(
Field::make( 'header_scripts', 'crb_header_script', __( 'Header Script', 'crb' ) ),
Field::make( 'footer_scripts', 'crb_footer_script', __( 'Footer Script', 'crb' ) ),
) );
Hope this helps.
Hello @htmlburger,
First off, thanks you all for the great plugin, it does me wonders, you answer sums it up pretty much, you can resolve the topic.
I have worked on this before you answered, I went with a spin on the first approach. I am using the Carbon Fields plugin, to make it so, the administrator can add roles and “fields” to the roles. For those who are interested, I made a couple of theme_options container. One to add remove roles, and one to add “fields” to the roles.
The first:
Container::make('theme_options','Plugin Options')
->add_fields(array(
Field::make('complex', 'roles')
->add_fields(array(
Field::make('text','display_name','Display name'),
Field::make('text','role_name','Role name'),
Field::make('complex','capabilities','Caps')
->add_fields(array(
Field::make('text','capabilities_text','Cap. name')))
))
));
The second:
$roles = carbon_get_theme_option('roles','complex');
$dynamic_roles_container = Container::make('theme_options','Plugin Roles Options')
->set_page_parent('Plugin Options');
for($i = 0; $i < count($roles); $i++) {
$dynamic_roles_container->add_fields(array(
Field::make('separator',$roles[$i]['role_name'] . 'role_separator',$roles[$i]['display_name'])));
}
Then you just call the fields by user role and ID.