Setting Maximum number of words a user can input into a post?

Community Forums Forums General Discussion Setting Maximum number of words a user can input into a post?

This topic is: resolved

Tagged: , , ,

This topic contains 2 replies, has 2 voices, and was last updated by  raindahl 8 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #91418

    raindahl
    Member
    Post count: 1

    Hi there ,

    I am new to Genesis themes and forums and would like to say hello !

    I am looking for help , basically I am building a website and my boss only wants there to be a limit of say 300 words that a user can input in a post/page and I have looked for plugins or code and have so far not found any ! The only ones really that I have found reduce the excerpt and the post title and I have tried to modify some of them but have had no joy

    This is the code I have been working on which I hoped would work :

    function maximum_number_words($content)
    {
    	global $post;
    	$content = $post->post_content;
    	if (str_word_count($content) > 300 )
    	wp_die( __('The current post is above the maximum number of words, it must be under 300 words.') );
    }
    add_action('publish_post', 'maximum_number_words');

    If anyone can help me with where I am going wrong or suggest a plugin it would be much appreciated because this is the last thing I have to do on the site!

    Thanks

    Andrew Rainey

    #91552

    essaysnark
    Participant
    Post count: 342

    I’m not the expert in this stuff but I’m pretty sure that you are using the wrong function – publish_post happens too late (it’s only triggered when the post has actually been published). Instead, I think you want to filter the content before save. Try this:

    http://codex.wordpress.org/Plugin_API/Filter_Reference/content_save_pre

    #92130

    raindahl
    Member
    Post count: 1

    Hi There ,

    Got it working just made a slight alteration to the code “publish_post” works fine now using this code :

    <?php
    function maximum_number_words($content)
    {
    	global $post;
    	$content = $post->post_content;
    	if (str_word_count($content) > 400 )  // maximum amount of words 
    	wp_die( __('The current post is above the maximum number of words, it must be under 400 words ') );
    }
    add_action('publish_post', 'maximum_number_words');
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.