• Hello!

    On looking around, there was a plugin (word count and limit) which you could use to put a maximum character count on any posts on your site, but this no longer works. I fed it back via the specific plugin forum.

    I also found some code, which a few years ago was exactly what I need right now to limit both post title and post size itself:

    function maxWord($title){
        global $post;
        $title = $post->post_title;
        if (str_word_count($title) >= 1 ) //set this to the maximum number of words
        wp_die( __('You can only use one word for the title!') );
    }
    add_action('publish_post', 'maxWord');
    
    function maxWord($content){
    global $post;
    $num = 500; //set this to the maximum number of words
    $content = $post->post_content;
    if (str_word_count($content) > $num)
     wp_die( __('Don't forget you can only use up to 500 words.  Try to trim it down a bit :)) );
    }
    add_action('publish_post', 'maxWord');

    Sadly, it appears this is no longer a working function… So my question is this. What can I do to limit the length of both titles and posts by myself and users, please? This is a pretty vital function for me.

    Many thanks!

    Pip

Viewing 4 replies - 1 through 4 (of 4 total)
  • Anonymous User 9055193

    (@anonymized-9055193)

    Hi there, I’ve used these filters (below) in the past, you can try playing around with these functions. Paste them into your theme’s function.php file and modify the $value

    Filter the_content():

    function example_filter_the_content( $content ) {
    	$value = 100;
    	return substr( $content, 0, $value );
    }
    add_filter( 'the_content', 'example_filter_the_content' );

    Filter the_title():

    function example_filter_the_title( $title ) {
    	$value = 10;
    	return substr( $title, 0, $value );
    }
    add_filter( 'the_title', 'example_filter_the_title' );

    Note: these filters will be applied to ALL posts.

    More reference for balancing tags, etc:
    https://stackoverflow.com/questions/3147898

    Cheers!

    Thread Starter MasterPip

    (@masterpip)

    Hi Tada!

    Thanks for that. The good news is that it’s managed to apply the functions without breaking the site (applying the previous function gave me a site-wide error which I had to fix on the server). It does also limit the length – though the values are characters rather than words.

    It also truncates rather than limits, and posts with no warnings this is happening…. It’s a great start though! At least I know counting and limiting is, in theory, still available…

    Does anyone else know of any filters more like the original?

    Ta ??

    Hello,

    And to answer you question, virtually there are no limits in size for any post or page, but if you put more than 200 000 lines, it’ll probably create issues (lag, so on). Truncating is already done by default and if not enough…
    issue

    Thread Starter MasterPip

    (@masterpip)

    Hi Digico.

    That is no answer to my question at all – I WANT to limit the posts as I know there is no current limit. I used to be able to do this with the WORDLIMIT function, or a plugin, but these no longer work. I’m looking for a way to limit the posts at the writing end, rather than the actual reading end.

    Thanks!

    Pip

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to limit the amount of words in a post?’ is closed to new replies.