Skip to content

Instantly share code, notes, and snippets.

@hazratbilal0079
Created September 29, 2024 06:49
Show Gist options
  • Save hazratbilal0079/f10edaf2567ac054cc6bbfc7c30e40c1 to your computer and use it in GitHub Desktop.
Save hazratbilal0079/f10edaf2567ac054cc6bbfc7c30e40c1 to your computer and use it in GitHub Desktop.
Add Custom role from frontend using php hook
hook name: registernewrole
Hook:
add_action( 'jet-form-builder/custom-action/registernewrole', 'register_new_user_role', 10, 2 );
function register_new_user_role( $request, $handler ) {
// Get the role name from the form field (assuming the field name is 'role_name')
$role_name = isset( $request['role_name'] ) ? sanitize_text_field( $request['role_name'] ) : '';
// Get the capabilities from the form field (assuming the field name is 'select_capabilities')
$capabilities = isset( $request['select_capabilities'] ) ? array_map( 'sanitize_text_field', (array) $request['select_capabilities'] ) : [];
// Check if role name and capabilities are provided
if ( empty( $role_name ) || empty( $capabilities ) ) {
return new WP_Error( 'missing_data', 'Role name or capabilities are missing.' );
}
// Define a unique role key (e.g., converting role name to lowercase and replacing spaces)
$role_key = strtolower( str_replace( ' ', '_', $role_name ) );
// Check if the role already exists
if ( get_role( $role_key ) ) {
return new WP_Error( 'role_exists', 'This role already exists.' );
}
// Add the new role with the provided capabilities
$result = add_role( $role_key, $role_name, array_flip( $capabilities ) );
// Check if the role was successfully created
if ( $result ) {
return 'Role successfully created!';
} else {
return new WP_Error( 'role_creation_failed', 'Failed to create the role.' );
}
}
Role Capabilities in Bulk:
Read: read
Create Posts: create_posts
Edit Posts: edit_posts
Delete Posts: delete_posts
Publish Posts: publish_posts
Edit Published Posts: edit_published_posts
Delete Published Posts: delete_published_posts
Edit Others' Posts: edit_others_posts
Delete Others' Posts: delete_others_posts
Manage Categories: manage_categories
Moderate Comments: moderate_comments
Upload Files: upload_files
Edit Private Posts: edit_private_posts
Delete Private Posts: delete_private_posts
Read Private Posts: read_private_posts
Create Pages: create_pages
Edit Pages: edit_pages
Delete Pages: delete_pages
Publish Pages: publish_pages
Edit Published Pages: edit_published_pages
Delete Published Pages: delete_published_pages
Edit Others' Pages: edit_others_pages
Delete Others' Pages: delete_others_pages
Edit Private Pages: edit_private_pages
Delete Private Pages: delete_private_pages
Read Private Pages: read_private_pages
Manage Options: manage_options
Edit Themes: edit_theme_options
Activate Plugins: activate_plugins
Install Plugins: install_plugins
Delete Plugins: delete_plugins
Update Plugins: update_plugins
Edit Plugins: edit_plugins
Install Themes: install_themes
Delete Themes: delete_themes
Update Themes: update_themes
Switch Themes: switch_themes
Manage Plugins: manage_plugins
Update Core: update_core
Customize: customize
List Users: list_users
Edit Users: edit_users
Delete Users: delete_users
Create Users: create_users
Promote Users: promote_users
Remove Users: remove_users
Add Users: add_users
Edit Dashboard: edit_dashboard
Edit Files: edit_files
Export: export
Import: import
Unfiltered HTML: unfiltered_html
Unfiltered Uploads: unfiltered_uploads
View Site Health: view_site_health
Manage Widgets: edit_theme_options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment