# Site Settings Object for Your Front-End Scripts

> Prints a small window.siteSettings object with the home URL, REST API root, AJAX URL, language and, for logged-in users, a REST nonce, so your custom JavaScript never hard-codes addresses.

- Type: PHP
- Runs: Frontend only
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: javascript, rest-api, helper
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-js-settings-object/

## Code

```php
add_action( 'wp_enqueue_scripts', function () {
    $data = array(
        'homeUrl'  => home_url( '/' ),
        'restUrl'  => esc_url_raw( rest_url() ),
        'ajaxUrl'  => admin_url( 'admin-ajax.php' ),
        'locale'   => determine_locale(),
        'loggedIn' => is_user_logged_in(),
    );

    // The nonce authenticates REST requests from logged-in users only.
    if ( is_user_logged_in() ) {
        $data['restNonce'] = wp_create_nonce( 'wp_rest' );
    }

    wp_register_script( 'wpide-site-settings', false, array(), '1.0', false );
    wp_enqueue_script( 'wpide-site-settings' );
    wp_add_inline_script( 'wpide-site-settings', 'window.siteSettings = ' . wp_json_encode( $data ) . ';', 'before' );
} );
```

## Notes

Read it in your scripts as window.siteSettings.restUrl; add your own values to the $data array.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Site Settings Object for Your Front-End Scripts" 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/
