• How to limit the content of WordPress posts so that if the character is less than 50 will not allow the post to be published?

    That is, the condition must be written that the user must write more than 50 characters in the posts to be able to publish the post

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The final enforcement of such a policy can be through the “wp_insert_post_data” filter. If the content is too short, you can prevent insertion by killing the process with wp_die(). Crude but effective. You’d also want a friendlier user experience which would be accomplished with JavaScript. It can do a character count when the publish button is clicked and put up a notice if the content is too short. But JavaScript measures are easily overridden, which is why we have fallback enforcement in server side PHP.

    Thread Starter mahdikhoram

    (@mahdikhoram)

    Thank you for your explanation.
    How do I use it function.php?

    Moderator bcworkz

    (@bcworkz)

    Filter hook code can go right in functions.php. If you’re not familiar with filter hooks, see https://developer.wordpress.org/plugins/hooks/filters/
    While that link is to the plugin handbook, the concept applies to theme functions.php as well.

    To use a filter hook, it helps to see how it is applied in code. wp_insert_post_data is applied here:
    https://core.trac.wordpress.org/browser/tags/5.6/src/wp-includes/post.php?marks=4009-4020#L4008
    Look above to line 3981 to see all of what is in $data.

    You may want to pass the content through wp_strip_all_tags() before counting with strlen() so some anchor or img tag doesn’t take up the character allotment without contributing any readable content.

    The JavaScript to provide a better user experience should reside in its own file. Load the file onto the page by enqueuing it with wp_enqueue_script(). This function needs to be called from a callback hooked to the “admin_enqueue_scripts” action. Action hooks are almost identical to filter hooks. There are some examples in the user notes at the bottom of the page:
    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress function code does not allow post contents of less than 50 characters’ is closed to new replies.