Support » Plugins and Hacks » [Resolved] length limit?

[Resolved] length limit?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Nevermind. It seems there was something odd about the data when pasted in that truncated it.

    Is there any way to stablish a lenght limit by characters?

    Thank you

    Plugin Author Steve



    As of the recently committed version 1.7.1, it is possible to limit the subheading displayed using the new subheading filter.

    For example, the following can be added to your theme functions.php file to limit the subheading to 5 words.

    add_filter( 'subheading', function( $value ) {
        return wp_trim_words( $value, 5 );
    } );

    If you require an exact number of characters, simply change the contents of the filter to manipulate the output to the desired format.

    Hope this helps.

    I’m sorry, I don’t know almost nothing about PHP. What shoud I do with this code? Just paste it in function.php? I’ve tried pasting this at the top of the file, but gives me error.

    <?php add_filter( 'subheading', function( $value ) {
        return wp_trim_words( $value, 5 );
    } ); ?>

    Parse error: syntax error, unexpected T_FUNCTION in (…)/functions.php on line 1

    Thanks in advance!

    Plugin Author Steve



    OK, not to worry – let’s try and solve this one.

    It could be and issue with PHP versions, the method used in the snippet (anonymous function) will only work in PHP 5.3 of greater.

    First option would be to check which version you are using, and then replace what you have with this alternative.

    function trim_subheading( $value ) {
        return wp_trim_words( $value, 5 );
    add_filter( 'subheading', 'trim_subheading' );

    If this isn’t the problem we can move on to something else.

    Now it doesn’t give an error, but is still not working. My vesion is greater than 5.3.

    Where should I notice the limit? In the subheading box, in the WordPress backend, accepting only five words? Or maybe it should let me write as many words as I want and later, in the frontend, show only the first five words?

    Thanks again!

    Plugin Author Steve


    OK, that’s sounds good to me.

    Using this filter will not limit the number of characters you can enter into the input on the edit page, it only has an affect on the final output.

    So you could enter “This is my example subheading that is too long.”, but using the filter with a 5 word limit will only display “This is my example subheading”.

    There are other options you can use with the wp_trim_words function, found in the Codex:


Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] length limit?’ is closed to new replies.