# Preconnect to Third-Party Origins

> Adds preconnect hints so the browser opens connections to external hosts (Google Fonts by default) earlier.

- Type: PHP
- Runs: Frontend only
- Category: [Performance](https://wpide.com/library/category/performance/)
- Tags: performance, preconnect, fonts
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/preconnect-resource-hints/

## Code

```php
add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
    if ( 'preconnect' !== $relation_type ) {
        return $urls;
    }

    // Origins your pages load files from.
    $origins = array( 'https://fonts.googleapis.com', 'https://fonts.gstatic.com' );

    foreach ( $origins as $origin ) {
        $urls[] = array( 'href' => $origin, 'crossorigin' => 'anonymous' );
    }

    return $urls;
}, 10, 2 );
```

## Notes

Only list origins that every page actually uses.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Preconnect to Third-Party Origins" 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/
