• Dusan

    (@afrofever23)


    Hi WPs,

    I recently added this short function in function.php because I wanted word count to send to data layer and via Google Tag Manager to collect it in Google Analytics.

    Everything went smoothly, data was collected but there is a difference between data sent to dataLayer regarding word count in blog post and data word count on Edit post page in Word Press interface.

    Do I need to modify function below to get accurate data because obviously function is count more than just words?

    function word_count() {
        $content = get_post_field( 'post_content', $post->ID );
        $word_count = str_word_count( strip_tags( $content ) );
        return $word_count;
    }

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi,
    Below is a bit of information on str_word_count().

    Read it carefully.

    For the purpose of this function, ‘word’ is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with “‘” and “-” characters.

    So, locale is important, make sure that receiver gets the right locale for the data you pass.

    Secondly, you can see exactly what words are found in your data by passing 2 to str_word_count() as the second argument.

    You can also compare output of the function with output given by command line program wc to see whether there are any discrepancies. wc -w gives number of words in a string.

    Hope this helps.
    [ Signature moderated ]

    • This reply was modified 8 years, 2 months ago by swww.
    • This reply was modified 8 years, 2 months ago by swww.
    • This reply was modified 8 years, 2 months ago by Steven Stern (sterndata).
    Thread Starter Dusan

    (@afrofever23)

    Thanks for the answer.

    Where can I see data from str_word_count() to compare it? I am not too familiar with php honestly.

    When you say wc -w you mean Word count under edit post area?

    The line below gives you total number of words in $content (tags stripped)

    
    $word_count = str_word_count( strip_tags( $content ) );
    

    Now, if you:

    
    print_r($word_count)
    

    , you’ll see number of words in $content.

    While speaking of wc utility, I had a command line program in my mind. It is available on Unix and Linux servers. You can try it out by logging in via ssh and typing something like:

    
    wc -w <<< "your content"
    

    The above will give you number of words between quotes, which is 2.

    [ Signature moderated ]

    • This reply was modified 8 years, 2 months ago by swww.
    • This reply was modified 8 years, 2 months ago by bcworkz.
    Thread Starter Dusan

    (@afrofever23)

    OK, but where do I need to put the line in WordPress interface to see the total number of words in $content?

    Moderator bcworkz

    (@bcworkz)

    Place the print_r() line within your function declaration just above the return line. The output will show up from where ever the function is called on a template. This may corrupt your google analytics code, depending on how the function is used, but you’ll see the result when viewing page source. It might also show up on the regular page display, probably at the bottom. The specifics depend on how and where the function is called.

    @swww – Thank you for helping out in the forums! Please stop placing your signature and site URL at the bottom of your posts. It is contrary to the forum guidelines. Thank you for understanding. We hope to see more of your helpful replies here!

    Thread Starter Dusan

    (@afrofever23)

    Thanks for the explanation. I understand your input but I don’t want to corrupt GA code.

    I will reformulate my question.

    How WordPress get those Word count under Post page and how can I rewrite the function to get that same number?

    Thanks a lot guys!

    Count of words currently in the textarea control of the editor is found using not PHP but JavaScript. One way to obtain it by yourself is with WordCounter object as follows:

    
    var counter = new window.wp.utils.WordCounter();
    var str = document.getElementById('content').value
    counter.count(str)
    

    What’s worth noting is, stuff like years or dates are not considered words by count(). Hence, the above code will return 6 for a string like this: in 2020 the world will have ended

    Similarly, the code below in PHP:

    
    print_r(str_word_count(strip_tags("in 2020 the world will have ended")));   
    

    will also return 6 as the number of words found (again years are not considered words by the function).

    So, at a glance, for a simple string there is no difference between how count() and your code written in PHP find number of words. You might want to test how things stand for more complex, formatted content.

    In conclusion, you probably want to trust count() rather than your PHP code on number of words in your post.

    Moderator bcworkz

    (@bcworkz)

    Using print_r() to investigate your code may be moot at this point, but I think it’s still worth noting that its use is intended to be temporary. Thus the chance of it corrupting your GA code is of little consequence. At worst, it will cause a JavaScript error. Not a big deal, you don’t really want GA to collect data from your usage anyway. The intent of print_r() is to be able to examine what a variable value is. Once that is known, you would remove or comment out the print_r() line, restoring normal functionality.

    The use of print_r() and its cousin var_dump() is a basic PHP debugging technique to check interim variables in code. It will invariably alter output somewhere in an undesirable manner. Because it is temporary, but it presents useful information, it is worth tolerating the temporary disruption of output.

    Thread Starter Dusan

    (@afrofever23)

    I understand.

    So basically, I can put print_r() in functions.php after my initial code and then I will see variable value at the end of the blog post text or not? I am curious where I would see print_r() value?

    Thanks a lot for the answer. Appreciate it really.

    I mentioned print_r() as the simplest way to see what’s going on in your code. You can use var_dump() as well. It will give you more comprehensive information. Try both function and see which of them you like more.

    In order not to search for your desired number of words on the screen you might want to terminate your script immediately after print_r() (or var_dump()) is called, which can be done like this:

    
    print_r($word_count);
    exit();
    

    This code can be put in functions.php, and its output as the last thing printed on the screen.

    For more information and examples on print_r(), var_dump() and exit() you might want to visit php.net .

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘WordPress Word Count Function’ is closed to new replies.