# X (Twitter) Handle Meta Tags

> Adds twitter:site with your brand's X handle to every page, and twitter:creator with the author's handle on posts, so shared links credit the right accounts.

- Type: PHP
- Runs: Frontend only
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: twitter, social, meta-tags
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/twitter-handle-meta-tags/

## Code

```php
// Your site's X handle, with the @.
$wpide_x_site_handle = '@yourbrand';

add_action( 'wp_head', function () use ( $wpide_x_site_handle ) {
    if ( '' !== trim( $wpide_x_site_handle ) ) {
        echo '<meta name="twitter:site" content="' . esc_attr( $wpide_x_site_handle ) . '" />' . "\n";
    }

    if ( is_singular( 'post' ) ) {
        $handle = trim( (string) get_the_author_meta( 'twitter', (int) get_queried_object()->post_author ) );

        if ( '' !== $handle ) {
            $handle = '@' . ltrim( preg_replace( '#^https?://(www\.)?(twitter|x)\.com/#i', '', $handle ), '@/' );
            echo '<meta name="twitter:creator" content="' . esc_attr( $handle ) . '" />' . "\n";
        }
    }
}, 6 );
```

## Notes

The author handle is read from a user meta field named twitter; leave it empty and only the site handle is used.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "X (Twitter) Handle Meta Tags" 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/
