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. Why would a GET variable one one page of a site cause a 404 error when a GET variable works on another page of a site?
  8. Where should I install a PHP library into WordPress so that code in a webpage can activate it?
  9. Check if user already visited this post today
  10. Nice scroll to wordpress
  11. Multiple pages on one with different HTML
  12. How can update custom meta for all posts
  13. Add custom HTML to posts page
  14. Minify HTML, CSS, JS with PHPWee?
  15. Blank space at beginning of tag?
  16. Can I change the contents of a description meta tag generated by Yoast?
  17. Batch update a post meta field value of each post in Post Type
  18. How to only show page id as body class?
  19. Shortcode content output but not in correct place
  20. How to properly insert a link to a template in WordPress?
  21. How can I hide Home in my menu in a specific page?
  22. Remove meta description on certain pages
  23. The text box have space character
  24. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  25. Need help with Google drive API [closed]
  26. Change title only in dynamic page
  27. Input data from email form not going to JSON file
  28. Archive post by meta value + 24hours
  29. Different post styles depending on category
  30. Show latest posts in a plain HTML website custom widget [closed]
  31. Category title output before opening title tag
  32. Hide HTML element (by class or ID) with PHP
  33. How to remove coupon dropdown feature
  34. Add html link in functions.php files of the theme
  35. Seo Friendly Filter URLs
  36. PHP contact form not redirecting [closed]
  37. How to Schedule Cronjobs for start of every month and year
  38. Handling PHP/HTML inside the output of a shortcode function
  39. Menu Custom Data Attributes
  40. Illegal string offset error in wordpress theme options textarea
  41. How do I access post_meta key with php?
  42. Separate array output into a
  43. Update post meta – Custom field does not match meta-key
  44. Need help removing […] after excerpt
  45. Initial offset of number of posts in home page on the posts page
  46. Genesis / custom template page / setting the page title and meta desc
  47. Define category ID using get_post_meta
  48. Using php inside javascript [closed]
  49. Update value of a associative array with update_post_meta
  50. Front end file upload returning wrong attachment url
  51. WordPressUser Submission Form
  52. Shortcode displaying at the top of the page [duplicate]
  53. WordPress custom post type and taxonomy
  54. Create a slug for a page in WordPress
  55. Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
  56. Time Delay a URL Redirect for Specific Page
  57. Cascading dropdown select search based on Parent Page & Child Page
  58. WordPress cascading dropdown selection search based on Parent Page & Child Page
  59. Custom URL for multiple categories hierarchy
  60. Order category posts by last word of custom field
  61. Background image no longer fits to screen now that project is in php files
  62. Adding HTML Code to Replace Text in PHP
  63. Shortcode in Shortcode/HTML Graphics and Pictures
  64. I can’t go to the page archive-{post_type}.php
  65. Help me I cannot link my font-awesome to my WordPress Theme. I have my font-awesome folder already in my project
  66. Use Search Term in html link and redirect
  67. Get and Update Most Meta Value as an array in HTML form
  68. Hide author from search results meta, still display date and categories
  69. Fix common misspellings/errors based on array of replacements
  70. HTML Formatting “single_term_title()”?
  71. Add function to head “no product found”
  72. How to center all text body in single.php at once?
  73. Collapsible menu on post sidebar only expands and does not collapse
  74. Unable to update the meta boxes with multiple fields
  75. How can I get my pagination loop to display the correct number of total pages?
  76. AMP version of e-commerce without plugin
  77. Adding user meta to post meta wp_insert_post() not working
  78. Creating Log-In Page for backend server
  79. user update profile for custom code
  80. Vertical Menu for one page only
  81. Bootstrap Carousel Indicators Won’t Cycle On Custom WordPress Theme
  82. Show post/page into div using function
  83. wordpress, Category ID
  84. Post from front end form to post_meta
  85. wp nav menu without label
  86. Pass the updated value of aid from form using ajax
  87. Order by in foreach
  88. Show contact 7 form in popup
  89. If custom field doesn’t exist, or exists and it’s true show title?
  90. Arrange Category post manually when displayed
  91. how to manipulate HTML parameters using PHP conditions
  92. Magento and WordPress URL integration [closed]
  93. getting image alt text via ACF plugin in WordPress [closed]
  94. How to edit homepage in WordPress?
  95. Metadata on the WP_Post object
  96. How to customize posts in WordPress by using HTML and CSS?
  97. Header location [closed]
  98. How to enqueue structured data as file – jsonld file seems not to work
  99. seo meta description issue in google search
  100. Slick slider not working on wordpress
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

  • Disable image compression for WebP
  • At what stage does wp_ajax hooks gets applied during WordPress request?
  • How to create specific wordpress Table
  • How to get variable from other function inside class function using add_action for Ajax call
  • Some images not loading after changing IP on local wordpress install
  • search posts by POST ID
  • Elementor Contact Form Submit button has empty class
  • Duplicate Slugs on multilingual site (with Polylang)
  • Timeline from diferent wordpress api urls endpoint works, but diferent date
  • $attributes not defined in block.json PHP template for ACF blocks
© 2023 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