I am looking to do some on-page SEO, wherein I want to limit the length of Title, Description, and more in my HTML meta tags.

Is there anyway I can access metatags already present in wp_head, and change their content.

For example, I want to ensure my title in <head> tags is 60 characters max.

But, if I have a Title length 90 characters, I want my actual <h1> to have full length, but the head tag should have <title> only 60 characters wrong. Same goes for descriptions, keywords, etc.

That's just an oversimplified example, though.

Adding additional tags in header.php is possible, but I suspect duplicate tags are a bad idea.

Is there any non-destructive way to do it? Looking for advice.

1 Answer 1


Sure, we can modify meta tags in WordPress using actions and filters.

The <title> tag is pretty easy, WP already has a filter called document_title_parts that allows you to manipulate the title before being outputted to the page. Here's a simplified example of using it :

add_filter( 'document_title_parts', 'filter_document_title_parts', 10, 1 );

function filter_document_title_parts( $title_parts ) {
    // Trims the title to a maximum of 60 characters
    $max = 60;
    if ( strlen( $title_parts['title'] ) > $max ) {
        $title_parts['title'] = substr( $title_parts['title'], 0, $max ) . '...';

    return $title_parts;

The meta description tag can be a bit more complex because WordPress doesn't have a functionality for meta descriptions, so if you have them you're most likely using a plugin. Yoast SEO and All In One are the most popular, so I'll give examples for them, they should cover most people.

Yoast SEO :

add_filter( 'wpseo_metadesc', 'filter_wpseo_metadesc', 10, 1 );

function filter_wpseo_metadesc( $metadesc ) {
    $max = 155;
    if ( strlen( $metadesc ) > $max ) {
        $metadesc = substr( $metadesc, 0, $max ) . '...';

    return $metadesc;

All In One SEO :

add_filter( 'aioseop_description', 'filter_aioseop_description', 10, 1 );

function filter_aioseop_description( $description ) {
    $max = 155;
    if ( strlen( $description ) > $max) {
        $description = substr( $description, 0, $max) . '...';

    return $description;

Same thing like the title - we just get the metadescription before the page is rendered and we manipulate them. You can modify the $max variable as you like.

Are there other metas you'd like modified? We can find a hook for them.

  • Hey thanks for it. Could you please advice me on how to modify other meta tags in head as well? I want to trim the meta description, and couple of other tags as well. Is there any generic way to find and modify any tag in wp_head
    – Amit Bajar
    May 26, 2023 at 18:08
  • The meta description should already be editable using the hooks I provided in the answer. What other hooks do you need? You can check the document_title_parts hook. If it doesn't have the hooks you want, you can just add a PHP variable to the meta value in header.php to trim it.
    – alex
    May 27, 2023 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.