Skip Navigation

Types API Filters

toolset_accepted_post_statuses_for_api

Description

Filter an array of post statuses that can be used in Toolset API functions. At the moment, this involves only the functions for retrieving the related posts.

Arguments
  • string[] $accepted_post_statuses Array of accepted post status values

Output

This filter returns an altered array of accepted post status values.

More Usage examples

Example
// Filter out posts that have the status set to 'trash'

add_filter( 'toolset_accepted_post_statuses_for_api', static function( $accepted_post_statuses ) {
    if ( is_array( $accepted_post_statuses ) ) { // just for safety...
        $accepted_post_statuses[] = 'trash';
    }
    return $accepted_post_statuses;
} );

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 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' );
 
// exclude "book-additional-notes" custom field from "book" custom post type.
add_filter( 'toolset_rest_expose_field', function(
    $expose_field, $domain, $group_slug, $field_slug, $element_type, $element_id
) {
    if( 'posts' === $domain && 'book' == $element_type && 'book-additional-notes' == $field_slug ) {
        return false;
    }
 
    // Different case - do not alter the result.
    return $expose_field;
}, 10, 6 );

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_relationship_connect_existing_options

Description

Defines a fixed list of options to offer when editing a post and setting a connection to other existing posts over a Types relationship. The filter then replaces the default search selector used for this purpose with a selector that only includes the fixed list of options.

Arguments
  • array $options List of options to define. Leave empty to continue using the default search selector. Each option should be an array with two keys: value should hold the post ID, and label can hold the post title.
    string $other_post_type` This parameter specifies the post type of the items offered in the selector. It corresponds to the reciprocal side of the relationship from the post currently being edited.
    string $relationship_slug This parameter defines the slug of the relationship for which the selector is being populated.
    int|false $current_post_id This holds the ID of the post currently being edited.

Output

This filter returns an array of arrays, where each entry has a value key (matching the desired post ID) and a label entry (providing a post identifier, like the post title).

More Usage examples

Example
// Preset and limit the companies allowed to create a new shop location, when `company` and `location` are post types linked by a `company-location` one-to-many relationship..
add_filter( 'types_relationship_connect_existing_options', function( $options, $other_post_type, $relationship_slug, $current_post_id ) {
if (
'company-location' === $relationship_slug
&& 'company' ===$other_post_type
) {
$options = [
[
'label' => 'Primark',
'value' => 123,
],
[
'label' => 'IKEA',
'value' => 143,
],
[
'label' => 'Carrefour',
'value' => 43,
],
];
}
return $options;
}, 10, 4 );

types_relationship_connect_existing_options[{relationship_slug}][{other_post_type}]

Description

This variable hook defines a fixed list of options to offer when editing a post and setting a connection to other existing posts over a Types relationship. It replaces the default search selector used for this purpose with a selector only including a list of the fixed options.

Arguments
  • array $options List of options to define. Leave empty to keep using the search selector. Each option should be an array with two keys: value should hold the post ID, and label can hold the post title.
    int|false $current_post_id The ID of the current post being edited.

Output

This filter returns an array of arrays, where each entry has a value key (matching the desired post ID) and a label entry (providing a post identifier, like the post title).

More Usage examples

Example
// Preset and limit the companies allowed to create a new shop location, when `company` and `location` are post types linked by a `company-location` one-to-many relationship..
add_filter( 'types_relationship_connect_existing_options[company-location][company]', function( $options, $current_post_id ) {
$options = [
[
'label' => 'Primark',
'value' => 123,
],
[
'label' => 'IKEA',
'value' => 143,
],
[
'label' => 'Carrefour',
'value' => 43,
],
];
return $options;
}, 10, 2 );

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 );