Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to modify default tags in wp_head()

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.

Related Posts:

  1. Header has extra HTML block at top before my code
  2. how to remove metadata from the posts of my blog?
  3. WordPress yoast seo plugin – parse snippet var in meta description
  4. How to add post meta in while loop?
  5. get_post_meta is always empty when I use wp_mail
  6. Custom HTML/CSS Theme into WordPress – SEO considerations
  7. Getting error when trying to implement site-wide breadcrumbs in Twentytwentyone Theme [closed]
  8. How to store data from multiple forms using ajax and php
  9. search and replace preword from author
  10. Add meta noindex to post if it has a specific taxonomy
  11. Embed html in php code
  12. How to remove role=”navigation” from all nav elements?
  13. Sort custom meta column by other meta value
  14. WP grandchild categories in nested ul li
  15. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  16. Use DOMDocument with ob_start breaks my HTML code
  17. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  18. Creating user status mode in WordPress
  19. Need help with Google drive API [closed]
  20. How to change redirection route to a php page for making it only accessible by logged-in members?
  21. selected option if current category is the value
  22. Change title only in dynamic page
  23. How to remove ?wp-mce-4103-20162941
  24. Customize position of social icons in upme plugin [closed]
  25. wp_head function not outputting
  26. Why Won’t My Inputs in a form with a Get method work together?
  27. parts of url disappear when using # inside href=”” [closed]
  28. Settings API callback function inserts html quotes from nowhere
  29. Input data from email form not going to JSON file
  30. How to get the value of input hidden html from text editor to custom page template?
  31. Archive post by meta value + 24hours
  32. Different post styles depending on category
  33. How create a Shortcode with hover and complex options
  34. How do I remove the product thumbnail link from a specific div in Woocommerce? [closed]
  35. Dot is not added at the end of href
  36. WordPress Multiple Navigation bars
  37. Position banner to right of logo in header – messed up my header file
  38. Commenting out unwated CSS/scripts in HTML with PHP
  39. WordPress update_post_meta updating with empty meta_value field [closed]
  40. How to separate HTML and WordPress files
  41. Add .htm extention to a custom taxonomy
  42. Login to wordpress by clicking a link and specifying usernaname and password in url
  43. Auto 301 to full post permalink? (using : /archive/%post_id% to %post_name% )
  44. How do I add a custom css to all posts without affecting homepage css? [closed]
  45. Make PHP work with HTML tags
  46. Noscript not working as it should in wordpress site
  47. Show latest posts in a plain HTML website custom widget [closed]
  48. How to convert select to HTML with PHP tags inside of it [closed]
  49. Is it possible to retrieve all posts with a certain value for metadata?
  50. Get post id within comments loop
  51. Is this spyware on my site or a real Java update?
  52. Syntax error when I try to insert my loop into an unordered list? [closed]
  53. How to make modifications only to certain elements of an HTML string on the server-side?
  54. Non-Closing PHP Query in WordPress Loop
  55. Wrapping a Sidebar within
  56. Category title output before opening title tag
  57. Search WordPress using static html page
  58. Hide HTML element (by class or ID) with PHP
  59. Add Codepen animation as Preloader to WordPress
  60. Path for php file for inserting data through html form
  61. Function returns text instead of html [closed]
  62. Printing HTML codes
  63. Widget back end radio button issue
  64. HTML escaping data with ajax requests
  65. How i can get post data from database when i am getting post id from url in wordpress
  66. How to retrieve the data from the sever and displaying it in a page?
  67. php within html within php
  68. How to execute html code inside php?
  69. Querying Database with wpdb
  70. How to remove coupon dropdown feature
  71. Add data-id attribute to child page links
  72. Add shortcode in my div
  73. update_post_meta only updating on last loop of foreach
  74. WordPress plug-in for dynamic download link?
  75. HTML table attributes ignored
  76. how to get serialized post meta
  77. php esc_html_e with an html link inside not working
  78. Add html link in functions.php files of the theme
  79. Using ob_start and ob_get_clean with wordpress shortcode
  80. How to use two meta_compare in an array?
  81. How to edit HTML of my website on WordPress? [closed]
  82. WordPress wp-admin redirect and exception
  83. How to remove the h6 tag for the entry-category Class
  84. Override user-edit.php to design own profile page
  85. wp_insert_post() with HTML tags using PHP
  86. How to rename woocommerce-billing-fields
  87. Prevent redirect to wp-login.php
  88. Seo Friendly Filter URLs
  89. PHP contact form not redirecting [closed]
  90. Creating an image from a custom field
  91. How to add no follow to specific links?
  92. How to Schedule Cronjobs for start of every month and year
  93. Where can I find the inline-css of my theme header?
  94. Remove empty terms from array, sort alphabetically, update back to repeating field
  95. WordPress loop by meta key that is an array? and how loop multiple arrays
  96. Handling PHP/HTML inside the output of a shortcode function
  97. Make textarea required. Help [closed]
  98. How to store multiple custom meta box
  99. WordPress forms submissions and PHP files
  100. Menu Custom Data Attributes
Categories PHP Tags html, php, post-meta, seo, wp-head
Using a form to pass GET parameters and create an array of posts accordingly
Add text below WooCommerce short description if metabox value is true

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress