Types API Filters

toolset_rest_expose_field

Description

Determine whether a particular custom field should be exposed in the REST API. The filter is applied only for fields which actually belong to the element.

Arguments
  • bool $expose_field_group True by default.

  • string $domain Domain of the field group: 'posts', 'users' or 'terms'.

  • string $group_slug Slug of the custom field group.

  • mixed $element_type Type of the element for which we're deciding. Depending on the domain, this can be:
    - post type slug
    - taxonomy slug
    - user role name or an array with user role names

  • int $element_id ID of the element.

Output

bool True if the field group should be exposed, false otherwise.

More Usage examples

Example
// This is to enable the filtering at all.
add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );

// Hide the following information from the REST API:
// - custom fields from a field group with the slug 'my_private_group' for all posts
// - all custom fields from posts of type with the slug 'my_secret_post_type'
add_filter( 'toolset_rest_expose_field_group', function(
    $expose_field_group, $domain, $group_slug, $element_type, $element_id
) {
    if( 'posts' === $domain && 'my_private_field_group' == $group_slug ) {
        return false;
    }

    if( 'posts' === $domain && 'my_secret_post_type' === $element_type ) {
        return false;
    }

    // Different case - do not alter the result.
    return $expose_field_group;
}, 10, 5 );

toolset_rest_expose_field_group

Description

Determine whether fields of a particular field group should be exposed in the REST API. The filter is applied only for field groups which actually belong to the element.

Arguments
  • bool $expose_field_group True by default.

  • string $domain Domain of the field group: 'posts', 'users' or 'terms'.

  • string $group_slug Slug of the custom field group.

  • mixed $element_type Type of the element for which we're deciding. Depending on the domain, this can be:
    - post type slug
    - taxonomy slug
    - user role name or an array with user role names

  • int $element_id ID of the element.

Output

bool True if the field group should be exposed, false otherwise.

More Usage examples

Example
// This is to enable the filtering at all.
add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );

// Hide the following information from the REST API:
// - custom fields from a field group with the slug 'my_private_group' for all posts
// - all custom fields from posts of type with the slug 'my_secret_post_type'
add_filter( 'toolset_rest_expose_field_group', function(
    $expose_field_group, $domain, $group_slug, $element_type, $element_id
) {
    if( 'posts' === $domain && 'my_private_field_group' == $group_slug ) {
        return false;
    }

    if( 'posts' === $domain && 'my_secret_post_type' === $element_type ) {
        return false;
    }

    // Different case - do not alter the result.
    return $expose_field_group;
}, 10, 5 );

types_sanitize_field_labels

Description

Disable the sanitization of field and field option labels (title, options for select, checkboxes and radio fields, etc.) when saving on the Edit Field Group page. We suggest using this filter only on existing pages that rely on HTML code in the field labels that were created with versions prior to Types 3.3.4.

Arguments
  • bool $sanitize True by default.

Output

bool True if the field labels should be sanitized, false otherwise.

More Usage examples

Example

You can add this as a Toolset code snippet.
Take a look at the documentation on using Toolset to add custom code.

/**
 * Deactivate the sanitization of custom field and field option labels, 
 * so that they can contain HTML code.
 *
 * Note that this is not recommended for sites that don't explicitly need it.
 * Disabling sanitization can have undesired security or other ramifications.
 */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'types_sanitize_field_labels', '__return_false' );

wpt_field_options

Description

The filter wpt_field_options can be used to manipulate the options of select and radio field.
This filter may be modified or replaced in a future Types release, use at your own risk.

Arguments
  • array $current_options An associative array with options for the field. The structure depends on the type of the field. See the example for more information.

  • array $title Title (label) of the field.

Output

This filter must return an array with the same structure as $current_options.

More Usage examples

Example
add_filter( 'wpt_field_options', function ( $current_options, $title_of_field ) {
    if ( $title_of_field != 'Headline Color' ) {
        // not our "Headline Color" field
        return $current_options;
    }

    $theme_colorset = get_option( 'theme_name_colorset' );

    if ( ! $theme_colorset ) {
        // no theme colors are set yet
        return array(
            array(
                '#title' => 'No colors available',
                '#value' => 0
            )
        );
    }

    $new_options = array();
    foreach ( $theme_colorset as $color_title => $color_value ) {
        $new_options[] = array(
            '#title' => $color_title . ' (' . $color_value . ')',
            '#value' => $color_value
        );
    }

    return $new_options;
}, 10, 2 );