Help > Forum > Topics & Posts > Change Word Limit for Post Messages
Change Word Limit for Post Messages
Post messages can contain a maximum of 65,000 characters, and currently, there is no built-in option to change this limit.
However, if you want to enforce a custom word limit for new topic messages and post replies, you can follow the instructions below to add a script that enforces the word limit.
The script will display a word counter message below the editor and will also show an alert on form submission if the word count exceeds the specified limit. This word limit will not apply to moderators and administrators.
- Log in to your Website Toolbox account.
- Click the Integrate link in the main menu.
- Click the HTML link.
- Copy the HTML code below into the Forum Head Tag HTML Code text box:
<script> var maximumWords = 100; window.addEventListener("load", function () { if (!isAdministratorOrModerator()) { if(jQ("#PostTopic").length) { waitForEditor("message", function () { setupWordCountObserver("message"); }); jQ("#post_submit, #previewpost_sbt").on("click", function () { if (showErrorMessage("message") === 1) { return false; } else { removeWordCountMessage("message"); } }); } if (jQ("#posts-list").length) { checkEditorLoaded(); jQ("#reply_submit, #submitreply").on("click", function () { if (showErrorMessage("message") === 1) { return false; } else { removeWordCountMessage("message"); } }); jQ(document).on("click", "[id^='save_']", function () { var savePostId = jQ(this).closest(".post-body-content").find("textarea").attr("id"); savePostId = savePostId.replace("message", ""); var postEditEditorId = "message" + parseInt(savePostId); if (showErrorMessage(postEditEditorId) === 1) { var editor = tinymce.get(postEditEditorId); if (editor && editor.startContent) { editor.setContent(editor.startContent); } return false; } else { removeWordCountMessage(postEditEditorId); } }); } } }); function checkEditorLoaded() { const observer = new MutationObserver((mutations, obs) => { for (const mutation of mutations) { for (const node of mutation.addedNodes) { if (node.nodeType === 1 && /^messaged*_ifr$/.test(node.id)) { var messageId = node.id.replace("_ifr", ""); waitForEditor(messageId, function () { setupWordCountObserver(messageId); }); } } } }); observer.observe(document.body, { childList: true, subtree: true }); } function waitForEditor(id, callback) { const interval = setInterval(() => { const editor = tinymce.get(id); if (editor && editor.initialized) { clearInterval(interval); callback(editor); } }, 100); } function setupWordCountObserver(id) { const editor = tinymce.get(id); if (!editor) return; showWordCountMessage(id); // Update message on any change editor.on("input", function () { showWordCountMessage(id); }); editor.on("change", function () { showWordCountMessage(id); }); editor.on("undo redo paste", function () { showWordCountMessage(id); }); } function isAdministratorOrModerator() { return jQ(".dropdown-menu .admin_user:not(.hidden)").length || jQ(".dropdown-menu .moderator_user:not(.hidden)").length; } function countWords(str) { return str .replace(/?a[^>]*>/g, "") .replace(/(<[^>]*>)+/gi, "") .replace(/&(.*?);/gi, " ") .replace(/(^\s*)|(\s*$)/gi, "") .replace(/\n/g, " ") .replace(/\s+/g, " ") .trim() .split(" ") .filter(Boolean) .length; } function showErrorMessage(id) { let postContent = ""; const editor = tinymce.get(id); if (editor) { postContent = editor.getContent(); } else { postContent = jQ("#" + id).val(); } const totalWord = countWords(postContent); if (!isAdministratorOrModerator() && totalWord > maximumWords) { wtbx.dialog.alert("Error: The text that you have entered is too long (" + totalWord + " words). Please shorten it to " + numberWithCommas(maximumWords) + " words."); return 1; } return 0; } function showWordCountMessage(id) { const editor = tinymce.get(id); if (!editor) return; const content = editor.getContent(); const totalWord = countWords(content); let message = ""; if (totalWord && totalWord <= 100) { message = "" + totalWord + ""; } else if (totalWord > 100) { message = "" + totalWord + ""; } if (message) { let wordText = "word"; if(totalWord > 1) { wordText = "words"; } message = "You\'ve typed " + message + " "+wordText+". Your submission must not exceed " + numberWithCommas(maximumWords) + " words."; } const containerId = "errorMessage" + id; const existingContainer = jQ("#" + containerId); if (!existingContainer.length) { const editorContainer = jQ(editor.iframeElement).closest(".mce-tinymce"); editorContainer.after("" + message + ""); } else { existingContainer.html(message); } } function removeWordCountMessage(id) { jQ("#errorMessage" + id).remove(); } function numberWithCommas(num) { return num.toString().replace(/B(?=(d{3})+(?!d))/g, ","); } </script> - Save the changes.
If you still need help, please contact us.