the problem with most plugins like that is it doesn’t strip out the html code when calculating, so
<strong>hi there</strong>
would actually count as 25 characters, and
<img src="http://yourdomain.com/wp-content/plugins/whatever/images/filename.jpg" width="100" height="100">
would count as 107 characters
Thread Starter
Jim S.
(@stitzelj)
Well, I don’t need a character count, but a word count. The content being submitted would be short fiction, so HTML markup would be minimal.
I’m not sure, I’ve seen jqueries that monitor the “comment” field, but not sure about the post one.
You want to hook the filter ‘wp_insert_post_data’. Your function will be passed an array with all the post data. If you assigned the array to $draft, you might be most interested in the value of $draft->post_content_filtered. Your function will need to somehow get a word count out of this value.
Unresolved is how to handle a high word count. I think if you just did a wp_die(), the user can just use their back button and all of the form data is still retained. Certainly, more elegant solutions are possible.
Could you use jQuery or something similar to disable the ‘publish’ button until the word count is acceptable?
You would still need a fallback for those who do not use Javascript, but this may be a good solution to start with.
For starters, this function would return the string without the HTML tags:
jQuery.fn.stripTags = function () {
return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, ''));
};
Thread Starter
Jim S.
(@stitzelj)
Doesn’t the editor already count the number of words in a post? How do I tap into that?