0

I have this function that helps me nicely trim post titles:

namespace Helpers;
..
function _s_trim_post_title ( $length = null, $delimiter = null ) {
    $title          = get_the_title();
    $trimmed_title  = mb_strimwidth( $title, 
                                     0, 
                                     $length === null ? BIG_INT : $length, 
                                     '' // Won't use, bugs out.
                                    );
    $url            = esc_url( get_permalink() );

    if( strlen( $title ) == strlen( $trimmed_title ) ) {
        $delimiter = '';
    }

    $delimiter = $delimiter === null ? '' : (string)$delimiter;

    $output = '<h2 class="post-title"><a href="' . $url . '" rel="bookmark">' . $trimmed_title . $delimiter . '</a></h2>';

    return $output;
}

I call it, on content.php as follows:

echo Helpers\_s_trim_post_title(24, '...');

And I was thinking, how about of doing all that with a filter:

add_filter( 'the_title', 'Helpers\\_s_trim_post_title', 24, '...');

And now, on my content.php, I'd replace all that stuff with the simple:

the_title()

Unfortunately, this breaks the whole server and sends it into a continuous loop.

Why?

1

2 Answers 2

1

Here's the correct code snippet. Hope it'll work for you.

Helper function

namespace Helpers;
..
function _s_trim_content( $content = '', $length = null, $delimiter = null ) {
    $trimmed_content = mb_strimwidth( $content, 
        0, 
        is_null( $length ) ? BIG_INT : $length, 
        '' // Won't use, bugs out.
    );

    if ( mb_strlen( $content ) === mb_strlen( $trimmed_content ) || is_null( $delimiter ) ) {
        $delimiter = '';
    }

    return $trimmed_content . $delimiter;
}

the_title filter

add_filter( 'the_title', function( $title, $post_id ) {
    if ( 'post' === get_post_type( $post_id ) ) {
        return \Helpers\_s_trim_content( $title, 24, '...' );
    }
    return $title;
}, 10, 2 );
2
  • Thanks so much, Obi! Thing is, reading about actions, this has a big problem: How do I un-filter it? This is an anonymous function that's not called in any way. Feb 14, 2018 at 23:00
  • You have to create named function to make it removable. add_filter( 'the_title', 'op_trim_the_title', 10, 2 ); function op_trim_the_title( $title, $post_id ) { ... } . To remove remove_filter( 'the_title', 'op_trim_the_title', 10 );
    – obiPlabon
    Feb 15, 2018 at 5:49
4

First of all, please try to understand how filter hooks work inside WordPress. These two functions will help you in this case apply_fitlers() and add_filter()

Then please check how the_title filter hook works and what it does and how it does.

the_title filter hook is used in lots of places. It's been used in front-end as well as backend and it only works with the title. But you added heading h2 tag and anchor a tag with it. You can use is_admin() conditional function to apply the filter only on front-end. So make sure your the_title filter callback only works with title and does not return any markup.

Your the_title filter hook callback Helpers\_s_trim_post_title() does not work with the arguments that it gets from the filter hook. And you called get_the_title() template tag inside the_title which became recursive call, an infinite loop! That's why your system isn't working as expected.

2
  • I've just started to dwelve into hooks / filters, this is of great help and I'm somewhat understanding how and where they should go. Would you please be able to provide some code as a fix? This could help me understand exactly where the pain points are and help me have the codex make even more sense with a practical exercise. Feb 11, 2018 at 12:18
  • @JonathanGuerin Added another answer please check.
    – obiPlabon
    Feb 12, 2018 at 6:20

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.