Does settings API create settings on run time?

I’m tweaking a plugin and I’d like to have an option value different for each backoffice user.

Currently the options are defined using the add_settings_field method that is performed in the admin_init hookup.

My question is, could I register a new set of settings by prefixing my settings using the current user ID?

Such a way, if valid, could help to solve other problems, such:

  • How to Set an Individual Homepage for Each User?
  • https://wordpress.stackexchange.com/questions/98060/settings-api-creating-setting-fields-on-the-fly

Solutions Collecting From Web of "Does settings API create settings on run time?"

Yes, you can and it’s easier using a Class. In the admin_init hook (where the Settings API is being registered and defined) set a class property based on the user ID:

$this->prefix = 'uid_' . get_current_user_id() . '_';

Then, in the rest of the code, refer to your option name as $this->prefix . 'option_name'.

The result in the table wp_options will be uid_1_option_name, uid_2_option_name, etc. And each user will have its own settings.

A Gist with a working example.