# Polylang Language Fields in the REST API

> Adds pll_language and pll_translations fields to posts and pages in the REST API, which Polylang's free version does not provide, so headless sites and apps can link translations.

- Type: PHP
- Runs: Run everywhere
- Category: [Multilingual](https://wpide.com/library/category/multilingual/)
- Tags: polylang, rest-api, headless
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/pll-rest-language-fields/

## Code

```php
$wpide_types = array( 'post', 'page' ); // Post types to extend.

add_action( 'rest_api_init', function () use ( $wpide_types ) {
    if ( ! function_exists( 'pll_get_post_language' ) ) {
        return;
    }

    register_rest_field( $wpide_types, 'pll_language', array(
        'get_callback' => function ( $post ) {
            $slug = pll_get_post_language( $post['id'] );
            return $slug ? array( 'slug' => $slug, 'locale' => pll_get_post_language( $post['id'], 'locale' ) ) : null;
        },
        'schema'       => array( 'type' => array( 'object', 'null' ), 'context' => array( 'view', 'edit' ) ),
    ) );

    register_rest_field( $wpide_types, 'pll_translations', array(
        'get_callback' => function ( $post ) {
            return (object) array_map( 'intval', (array) pll_get_post_translations( $post['id'] ) );
        },
        'schema'       => array( 'type' => 'object', 'context' => array( 'view', 'edit' ) ),
    ) );
} );
```

## Notes

Add custom post types to $wpide_types. The translations field maps language slugs to post IDs.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Polylang Language Fields in the REST API" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
