• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

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

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

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

This topic is: resolved

Tagged: code, Funtion, limit, Words

  • This topic has 2 replies, 2 voices, and was last updated 9 years, 1 month ago by raindahl.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • February 20, 2014 at 10:42 am #91418
    raindahl
    Member

    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

    February 20, 2014 at 10:46 pm #91552
    essaysnark
    Participant

    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

    February 24, 2014 at 8:54 am #92130
    raindahl
    Member

    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');
    ?>
  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble