Using Gravity Forms ‘gform_rest_api_capability_post_forms’ PHP filter

The gform_rest_api_capability_post_forms filter allows you to modify the capability required to create forms via the Gravity Forms REST API v2.

Usage

add_filter('gform_rest_api_capability_post_forms', 'your_function_name', 10, 2);

Parameters

  • $capability (string): The capability required. Defaults to ‘gravityforms_create_form’.
  • $request (WP_REST_Request): Full data about the request.

More information

See Gravity Forms Docs: gform_rest_api_capability_post_forms

Examples

Change the required capability

This example changes the required capability to create forms via the REST API to ‘my_custom_capability’:

add_filter('gform_rest_api_capability_post_forms', 'get_post_forms_capability', 10, 2);

function get_post_forms_capability($capability, $request) {
    return 'my_custom_capability';
}

Allow all users to create forms

This example allows all users to create forms via the REST API, regardless of their capability:

add_filter('gform_rest_api_capability_post_forms', 'allow_all_users_to_create_forms', 10, 2);

function allow_all_users_to_create_forms($capability, $request) {
    return true;
}

Allow only administrators to create forms

This example allows only administrators to create forms via the REST API:

add_filter('gform_rest_api_capability_post_forms', 'allow_only_admins_to_create_forms', 10, 2);

function allow_only_admins_to_create_forms($capability, $request) {
    return current_user_can('manage_options');
}

Allow specific user role to create forms

This example allows users with the ‘editor’ role to create forms via the REST API:

add_filter('gform_rest_api_capability_post_forms', 'allow_editors_to_create_forms', 10, 2);

function allow_editors_to_create_forms($capability, $request) {
    return current_user_can('editor');
}

Check for custom user meta to allow form creation

This example allows form creation via the REST API if the user has a specific custom meta value:

add_filter('gform_rest_api_capability_post_forms', 'allow_custom_meta_users_to_create_forms', 10, 2);

function allow_custom_meta_users_to_create_forms($capability, $request) {
    $user_id = get_current_user_id();
    $can_create = get_user_meta($user_id, 'can_create_forms', true);

    return $can_create === 'yes';
}