• Resolved vsp

    (@vsp)


    Is there a PHP plugin or script that I could use to automatically generate the wordcount of a post, which could then be displayed somewhere on the page of the post?

    The only plugins that I have found so far only calculate the word count for a category or the entire blog.

    (sorry for the double post)

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Add this function to your theme’s functions.php:

    <?php
    function wordcount($str) {
    return count(explode(" ",$str));
    }
    ?>

    Then, in your loop or wherever you have the_content(), add this code where you want the wordcount displayed:
    echo wordcount(get_the_content());

    Thread Starter vsp

    (@vsp)

    How exactly am I supposed to add that to functions.php? I tried to put it at the bottom of the code, but then when I tried to add the display code to the page layout, i got an error, and it wouldnt let me edit the theme. any ideas?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    You open the functions.php file in a text editor.

    You copy and paste the code into the file. You can leave off the <?php and ?> bits if you already have them in there.

    You make sure that you don’t have any blank lines or space or anything else outside of the <?php and ?> bits in the functions.php file. It needs to be pure PHP, no output at all.

    You then modify your other theme files as needed.

    BTW, if you don’t know how PHP works, then you really should not be messing with this sort of thing anyway. Might I suggest reading some PHP tutorials online? It might help you understand what’s going on. My guess is that you have added something to functions.php that causes it to have actual output, like a blank line or spaces at the beginning or end of the file. You should remove those. The file must start with <?php and end with ?> and the functions themselves, including the one you’re adding, should all be in between.

    Thread Starter vsp

    (@vsp)

    excellent, i got it working. i have some knowledge of PHP, but it’s not extensive.

    i have a second question: would it be possible to edit the code to make it round to the nearest hundred?

    Thread Starter vsp

    (@vsp)

    Disregard that last post, I figured out the code to get it to round. If anyone is interested, It’s this:

    $wordct = wordcount(get_the_content());
    echo round($wordct,-2);

    Change the value of the -2 to change what it rounds to (-3 will round to the nearest thousand, 3 will round to the nearest thousandth, etc.)

    It may be worthwhile to consider two different PHP functions: str_word_count and strip_tags.

    The exploding trick is not very smart about non-literal-space whitespace characters (although I don’t think tabs ever get into WordPress entries). I don’t know it well enough to know its weaknesses, but I’m going to guess that the actual PHP str-word-count command has been designed to be maximally elegant and efficient. The only disadvantage to using it is that the function requires PHP >= 4.3.0. Since WordPress itself requires version 4.2 or higher. If your host has the appropriate version of PHP, great. If you want a general solution, you might test the PHP version first and create a fall-back to the string exploding scenario.

    The strip_tags option should be self-explanatory. I know that many of my posts are full of lists, the links have full title strings added, etc. If you only care for your word count to be ballpark, maybe you don’t care, but I would suggest using strip_tags for that last bit of cleanliness. Note that strip_tags is supported in all PHP 4+ versions.

    (As an update to what I just wrote an hour ago.)

    I’ve found a few flaws in the code recommended above. First of all, the use of the get_the_content function was only returning the content before the <!--more--> tag if I used it in a multi-post (ie. index.php) file. I rewrote the function to look like this:

    function wordcount() {
      global $page, $pages;
      if (!function_exists('str_word_count')) {
        return str_word_count(strip_tags($pages[$page-1]));
      } else {
        return count(explode(" ",strip_tags($pages[$page-1])));
      }
    }

    and I called it from within “The Loop” by

    <?php _e('with Word Count:' . wordcount()); ?>

    instead of the simple “echo” statement that was mentioned. (‘_e’ runs string echoes through a nationalization process and is a bit more elegant.)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Wordcount plugin for each post?’ is closed to new replies.