• Open b2templates.funtions.php and find this in your POST TAGS section, it’s around line 599 depending on the hacks you already have in there:

    function get_the_content($more_link_text='(more...)', $stripteaser=0, $more_file='') {
    global $id,$postdata,$more,$c,$withcomments,$page,$pages,$multipage,$numpages;
    global $HTTP_SERVER_VARS, $preview;
    global $querystring_start, $querystring_equal, $querystring_separator;
    $output = '';
    if ($more_file != '') {
    $file=$more_file;
    } else {
    $file=$HTTP_SERVER_VARS['PHP_SELF'];
    }
    $content=$pages[$page-1];
    $content=explode('<!--more-->', $content);
    if ((preg_match('/<!--noteaser-->/', $postdata['Content']) && ((!$multipage) || ($page==1))))
    $stripteaser=1;
    $teaser=$content[0];
    if (($more) && ($stripteaser))
    $teaser='';
    $output .= $teaser;
    if (count($content)>1) {
    if ($more) {
    $output .= ''.$content[1];
    } else {
    $output .= '<span id="varP'.$id.'">
    read more »</span><div id="varXYZ'.$id.'" style="display: none">'.$content[1].'
    « hide more</div>';
    }
    }
    if ($preview) { // preview fix for javascript bug with foreign languages
    $output = preg_replace('/\%u([0-9A-F]{4,4})/e', "'&#'.base_convert('\\1',16,10).';'", $output);
    }
    return($output);
    }
    And insert this right after that:
    function get_words() {
    $content = get_the_content($more_link_text,$stripteaser,$more_file);
    $content = convert_bbcode($content);
    $content = convert_gmcode($content);
    $content = convert_smilies($content);
    $content = convert_chars($content,"html");
    $stripper = "";
    $wordcount = strip_tags($content,$stripper);
    $wordcount = explode(" ",$wordcount);
    $wordcount = count($wordcount);
    echo "$wordcount words in this post";
    }

    Call this function in your index.php with:
    <?php get_words()?>

Viewing 15 replies - 16 through 30 (of 45 total)
  • Yeah… What I get when I use it on my main blog page is the total number of words appearing on just that page.
    So, for a 100-word post with 25 words before <!–more–>, I get a count of 25 words on my main blog page and either 75 or 100 on my single post page (depending on whether I’ve also used <!–noteaser–>).
    That make any sense?
    Thanks
    Devan

    Thread Starter southerngal

    (@southerngal)

    Weird! Maybe something in the code can be changed to reflect that there is more to the post.

    I’d like to think so, too, but I beyond staring at it in b2template.functions.php I haven’t a clue where to start (as I don’t really know PHP)… Any suggestions?

    Thread Starter southerngal

    (@southerngal)

    I have no idea! I just found the code and posted it here. I’m sure someone will post after me with the fix. ??

    There’s a related conversation that’s picked up over here

    I’m using version 1.0-alpha-2, and I’d like to use Matt’s function of just keeping track of total words in *all* posts (see above). I’ve tried putting it in template.functions.php, as well as functions.php, but neither displays anything when I put the function call in my index.php. (By the way, should it be <?php get_words(); ?> or without the semi-colon?)
    The good news is it doesn’t crash anything, it just doesn’t display. I did check the database table names that it’s referring to, in case there’d been any change, but all that looks okay. Help?

    *bumps because I really really want to use this function*

    Thread Starter southerngal

    (@southerngal)

    I put mine in commentposts.php
    <?
    ⁄* b2stats.php
    * An addon to B2 which diplays b2 stats
    * Demo of this file @ http:⁄⁄www.orientek.net⁄kore⁄gamerz⁄b2stats.php
    * Created By Lester “GamerZ” Chan – http:⁄www.gamerz.per.sg
    * Copyright ? 2002 Lester “GamerZ” Chan. All Rights Reserved.
    *
    * B2 Created By Michel V – http:⁄⁄www.cafelog.com⁄
    *
    * You may modify this file to suit your owns needs, but please keep the copyright intact. Thanks
    * Any problems, email me @ [email protected] or post it in the B2 forums @ http:⁄⁄tidakada.com⁄board⁄
    *⁄
    ⁄⁄ Start Connect To MYSQL
    require(“wp-config.php”);
    include(“b2-include⁄b2vars.php”);
    ⁄** Get Total Number of posts
    ** Params: None
    ** Returns: number of posts
    *⁄
    function stats_get_totalposts() {
    global $tableposts;
    $sql = “SELECT COUNT(ID) FROM $tableposts”;
    $result = mysql_query($sql) or die(mysql_error());
    return mysql_result($result, 0);
    }
    ⁄** Get Total Number of comments
    ** Params: None
    ** Returns: number of comments
    *⁄
    function stats_get_totalcomments() {
    global $tablecomments;
    $sql = “SELECT COUNT(DISTINCT comment_ID) FROM $tablecomments”;
    $result = mysql_query($sql) or die(mysql_error());
    return mysql_result($result, 0);
    }
    $words = $wpdb->get_results(“SELECT post_content FROM $tableposts”);
    foreach ($words as $word) {
    $post = strip_tags($word->post_content);
    $post = explode(‘ ‘, $post);
    $count = count($post);
    $totalcount = $count + $oldcount;
    $oldcount = $totalcount;
    }
    ⁄⁄ Check $user isset
    if(!isset($user) || $user == “”) {
    ⁄⁄ Output General Stats
    echo “<b>”.stats_get_totalposts().”<⁄b> Posts<br>\n”;
    echo “<b>”.stats_get_totalcomments().”<⁄b> Comments<br><br>\n”;
    echo ‘Total words typed:<b>’. number_format($totalcount);
    echo “<b><⁄b><br>\n”;
    }
    else {
    ⁄⁄ ———— If $user set ———— ⁄⁄
    ⁄⁄ SNIP… Same as previous version
    } ⁄⁄ end else user set
    ?>
    Of course this board trashed the forward slashes and backslashes, but you get the idea. Is that what you wanted?
    Just call it in: <?php include(‘⁄home⁄username⁄public_html⁄includes⁄commentposts.php’); ?>

    SG, I appreciate the response, but here’s the thing: I’m very very stupid.
    This is what I need to be told in order to implement any hack:
    It works with the CVS (1.0-alpha-2 as of this moment).
    Tell me exactly where to put it. (what file, where in that file).
    Tell me exactly how/where to use/call it.
    Here’s what I’d like to have: total words in all posts (like you did for your nanowrimo blog). Right now, I’m not concerned with number of posts/comments/users/etc. I just want to keep track of how many words I’ve written.
    Is this what your script does? (I suppose the formatting issues didn’t help anything in trying to make sense of it. Do you think I could impose on you to just email it to me? (cenaATcox-internetDOTcom)

    Thread Starter southerngal

    (@southerngal)

    Mailing it to you. ?? I put it in with my total comments and entries posted.

    Does anyone see anything wrong with the ‘words’ case? It is calculating the number of total words posted, but also gives an SQL error.
    function get_stats($what) {
    global $wpdb;
    switch($what) {
    case ‘words’:
    global $tableposts;
    $words = $wpdb->get_results(“SELECT post_content FROM $tableposts”);
    foreach ($words as $word) {
    $post = strip_tags($word->post_content);
    $post = explode(‘ ‘, $post);
    $count = count($post);
    $totalcount = $count + $oldcount;
    $oldcount = $totalcount;
    }
    $request = number_format($totalcount);
    break;
    case ‘posts’:
    global $tableposts;
    $request = “SELECT COUNT(*) FROM $tableposts WHERE post_status = ‘publish'”;
    break;
    case ‘comments’:
    global $tablecomments;
    $request = “SELECT COUNT(*) FROM $tablecomments”;
    break;
    }
    echo $wpdb->get_var($request);
    }
    I have this in the my-hacks.php file and am calling it from index.php. The hack file option is turned on in the admin.
    Thanks!

    I split the word count off into it’s own function and it works perfectly now. Go figure… :-/

    Can someone please confirm if this hack works with 1.01 RC1?
    Has anybody written this up and posted it on their site? If so, can it please be added to the Wiki?
    Thanks.
    Craig.

    I have this function in my hack file and it works in 1.01 RC1
    function get_word_count() {
    global $wpdb, $tableposts;
    $words = $wpdb->get_results("SELECT post_content FROM $tableposts");
    foreach ($words as $word) {
    $post = strip_tags($word->post_content);
    $post = explode(' ', $post);
    $count = count($post);
    $totalcount = $count + $oldcount;
    $oldcount = $totalcount;
    }
    echo number_format($totalcount);
    }

    Added to Wiki.

Viewing 15 replies - 16 through 30 (of 45 total)
  • The topic ‘Word Count’ is closed to new replies.