Forums

[resolved] Live Preview + Live Word Count (6 posts)

  1. chipgrafx
    Member
    Posted 7 years ago #

    I'm looking for a Live Word Count that would work together with the Live Comment plugin. I've tried using Textarea Tools but it doesn't seem to work for me. Perhaps the JavaScript isn't working, I'm not sure.

    I'm fairly new to PHP (still in school) otherwise I would just write a plugin myself. Any help or suggestions would be appreciated.

  2. tdw
    Member
    Posted 7 years ago #

    you'll need to edit live-comment-preview.php, and the comment form for your theme -- probably called comments.php.

    In comments.php add the code for your counter... it can be code of your choosing so long as it has an id:

    word count <span id="wordcount">n/a</span>

    In live-comment-preview.php add the following functions just above the event listener (about line 144) :

    function initWordCount() {
    if(!document.getElementById)
    return false;


    var cmnt = document.getElementById('comment');
    var cnt = document.getElementById('wordcount');


    if ( cnt && cmnt)
    cmnt.onkeyup = function() {
    cnt.innerHTML = numWords(this.value);
    }
    }


    function numWords(string)
    {
    string = string + ' ';
    string = string.replace(/^[^A-Za-z0-9]+/gi, "");
    string = string.replace(/[^A-Za-z0-9]+/gi, " ");
    var items = string.split(" ");
    return items.length -1;
    }

    add an event listener (about line 184):
    addEvent(window, "load", initWordCount);

    This is untested, but it should work...

  3. tdw
    Member
    Posted 7 years ago #

    This is tested... you need to use the addEvent function so you're not stomping over the existing preview event. Replace function initWordCount with:

    function initWordCount() {
    if(!document.getElementById)
    return false;

    var cmnt = document.getElementById('comment');
    var cnt = document.getElementById('wordcount');

    if ( cnt && cmnt)
    addEvent(cmnt, 'keyup',wordcount);
    }

    function wordcount( )
    {
    var cnt = document.getElementById('wordcount');
    cnt.innerHTML = numWords(this.value);
    }

  4. tdw
    Member
    Posted 7 years ago #

    I also note that live-comment-preview.php generates a php error that prevents the script from working. Comment out line 24:$livePreviewDivAdded == false; (evaluates to false by default) and it works fine.

  5. chipgrafx
    Member
    Posted 7 years ago #

    Thanks tdw!! It works great. I had no problems making the changes, very smooth. Thanks again for the help.

    If you want to check it out (and maybe provide a little feedback)... chipgrafx

  6. peterchen
    Member
    Posted 6 years ago #

    Hi i'm sorry for bringing up an old thread like this but this function mentioned here is exactly what i've been looking a long time for =)

    My only problem is i can't get this to work...
    How can i include this live wordcount in the admin's "write post" panel? So that it counts the words i'm typing in the WYWISIG editor live ??

    I wan't to see and so my authors too how much words are already typed so this where a really improvement for me.

    Hoping someone can help me with this issue.

    P.S I'm on the newest wordpress release 2.02 i think

Topic Closed

This topic has been closed to new replies.

About this Topic