Combining Hooks for a Mini Project

·

·

Hooks—actions and filters—allow you to extend WordPress without touching core files. In this mini project, we’ll combine both to:

  1. Compute and store reading time on post save
  2. Display a reading-time badge in post content
  3. 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_post triggers when a post (post type post) is saved.
  • wp_strip_all_tags removes HTML to count actual words.
  • update_post_meta stores 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_meta retrieves 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 999 ensures this filter runs after any other excerpt modifications.

📌 Summary of Hooks Used

TaskHookTypePurpose
Compute reading time on savesave_post_postActionStore reading time in post meta
Display reading-time badgethe_contentFilterAppend badge HTML to post content
Shorten excerpts on archivesexcerpt_lengthFilterControl length of archive summaries

✅ Tips for Implementation

  1. Use appropriate hooks for context:
    • save_post_post for post meta updates
    • the_content for front-end content modifications
    • excerpt_length for archives
  2. Always return data for filters: Forgetting return $content; will break post output.
  3. Style your badge:
.reading-time {
    font-weight: bold;
    color: #555;
    margin-bottom: 10px;
}
  1. 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.