Hooks—actions and filters—allow you to extend WordPress without touching core files. In this mini project, we’ll combine both to:
- Compute and store reading time on post save
- Display a reading-time badge in post content
- Shorten excerpts on archive pages
📌 1. Compute and Save Reading Time
We want to calculate reading time every time a post is saved and store it in post meta.
🔹 Example
add_action( 'save_post_post', function( $post_id, $post, $update ){
// Count words in the post content
$words = str_word_count( wp_strip_all_tags( $post->post_content ) );
// Estimate reading time (200 words per minute)
$minutes = max( 1, ceil( $words / 200 ) );
// Save reading time in post meta
update_post_meta( $post_id, '_reading_time', $minutes );
}, 10, 3 );
Explanation:
save_post_posttriggers when a post (post typepost) is saved.wp_strip_all_tagsremoves HTML to count actual words.update_post_metastores the reading time under_reading_time.
✅ Tip: Use max(1, ...) to avoid showing 0 minutes for very short posts.
📌 2. Display Reading-Time Badge in the Content
Once reading time is saved, we can display it at the top of the post using the the_content filter.
🔹 Example
add_filter( 'the_content', function ( $content ) {
if ( is_singular( 'post' ) ) {
$minutes = (int) get_post_meta( get_the_ID(), '_reading_time', true );
if ( $minutes ) {
$badge = sprintf( '<p class="reading-time">⏱ %d min read</p>', $minutes );
// Concatenate badge and content
return $badge . $content;
}
}
return $content;
});
Explanation:
is_singular('post')ensures the badge only appears on single post pages.get_post_metaretrieves the reading time stored earlier.- Use
.for string concatenation in PHP (not+).
✅ Tip: Style the badge with CSS for a consistent look.
📌 3. Shorter Excerpts on Archive Pages
To make archive pages cleaner, we can shorten post excerpts using the excerpt_length filter.
🔹 Example
add_filter( 'excerpt_length', fn($length) => 24, 999 );
Explanation:
- Sets all archive excerpts to 24 words.
- Priority
999ensures this filter runs after any other excerpt modifications.
📌 Summary of Hooks Used
| Task | Hook | Type | Purpose |
|---|---|---|---|
| Compute reading time on save | save_post_post | Action | Store reading time in post meta |
| Display reading-time badge | the_content | Filter | Append badge HTML to post content |
| Shorten excerpts on archives | excerpt_length | Filter | Control length of archive summaries |
✅ Tips for Implementation
- Use appropriate hooks for context:
save_post_postfor post meta updatesthe_contentfor front-end content modificationsexcerpt_lengthfor archives
- Always return data for filters: Forgetting
return $content;will break post output. - Style your badge:
.reading-time {
font-weight: bold;
color: #555;
margin-bottom: 10px;
}
- Combine hooks safely: Make sure your actions and filters do not conflict with other plugins modifying content.
This mini project demonstrates how actions and filters work together to create dynamic, user-friendly WordPress functionality.
