Recent Comments

Tags: , June 25, 2005 (7 comments)

Disclaimer: This plugin is no longer maintained. Feel free to take what there is under the GPL and do anything you want with it.

Well, I finally think my first WP plugin is stable enough to be released to the public :), after a couple of months of testing. To my knowledge there are no bugs in the current version (1.0.1), and I think I've tested it enough.

Code

Download .zip

<?php/*Plugin Name: Recent CommentsPlugin URI: http://blog.geoffers.uni.cc/archives/2005/06/24/recent-comments/Description: Retrieves a list of the most recent comments.Version: 1.0.1Author: Geoffrey SneddonAuthor URI: http://geoffers.uni.cc*/ 
 function print_gwp_recent_comments ($no_post = 5, $before = '<li>', $after = '</li>'){    global $wpdb;    $comments = $wpdb->get_results("SELECT `comment_ID` , `comment_post_ID` , `comment_author` FROM `$wpdb->comments` WHERE `comment_approved` = '1ORDER BY `comment_date_gmt` DESC LIMIT 0 , $no_post");    $output = '';    if ($comments)    {        foreach ($comments as $comment)        {            $id = $comment->comment_post_ID;            $output .= $before . '<a href="' . get_permalink($id) . '#comment-' . $comment->comment_ID . '">' . $comment->comment_author . '</a><br /><span class="post">(<a href="' . get_permalink($id) . '" title="Permalink: ' . htmlspecialchars(get_the_title($id)) . '">' . get_the_title($id) . '</a>)</span>' . $after . "\n";        }    }    echo $output;} ?>

Usage

<?php print_gwp_recent_comments(number_of_comments, 'before', 'after'); ?>

Example

<?php print_gwp_recent_comments(10, '<p>', '</p>'); ?>

Parameters

number_of_comments
(integer) Number of comments to display. Defaults to 5.
before
(string) Text to place before the title. Defaults to '<li>'.
after
(string) Text to place after the title. Defaults to '</li>'.

Combating Spam

Tags: , May 19, 2005 (3 comments)

Disclaimer: This is deprecated by my key spam plugin (which is no longer maintained).

Stage 1

  1. Open /wp-content/themes/yourtheme/comments.php
  2. Find name="author"
  3. Replace author with any old unlikely string
  4. Make a note of your any old unlikely string
  5. Save and Close /wp-content/themes/yourtheme/comments.php

Stage 2

  1. Open /wp-comments-post.php
  2. Find trim($_POST['author']);
  3. Replace author with your any old unlikely string (must be the same string as in stage 1)
  4. Save and Close /wp-comments-post.php

Easy CSV

Tags: March 24, 2005 (0 comments)

This more or less does exactly what it says on the tin: parse CSV into a PHP array. It has serveral advantages over fgetcsv, such as, even if auto_detect_line_endings is set as false in php.ini, it can still cope with all Windows, Mac and *nix line breaks. As basslines at gmail dot com says, it also can't cope with the PHP escape character in the CSV, while this can. That's all I can think of off the top of my head...

To call the function, you can use something like easy_csv (file_get_contents('2005.csv')); if your CSV uses the default "," delimiter and is at 2005.csv, to specify a non-default delimiter, you would use something like easy_csv (file_get_contents('2005.csv'), ';'); if you're CSV uses the ";" delimiter.

You can, of course, put the CSV directly in the function, like easy_csv ('Australia, Malaysia, Bahrain, San Marino, Spain, Monaco');

As for the code itself, here you go:

<?php // Define the function easy_csvfunction easy_csv ($csv, $delimiter = ',') { // If it's got a \n line break (Windows style) then seperate lines into an enumerated arrayif (strpos($csv, "\r\n")) {$csv_lines = explode("\r\n", $csv); // If it's got a \r line break (Mac style) then seperate lines into an enumerated array} elseif (strpos($csv, "\r")) {$csv_lines = explode("\r", $csv); // If it's got a \n line break (*nix style) then seperate lines into an enumerated array} elseif (strpos($csv, "\n")) {$csv_lines = explode("\n", $csv); // If it's not got any of the above line breaks, then just dump the whole thing into $cvs_lines} else {$csv_lines = $csv;} // If $cvs_lines is an array (aka. has more than one line)if (is_array($csv_lines)) { // Set $i to 0$i = 0; // Loop through the linesforeach ($csv_lines as $csv_line) { // Put each piece of data as part of the array$csv_lines[$i] = explode($delimiter, $csv_line); // Add 1 to $i$i++; // End foreach ($csv_lines as $csv_line)} // If it isn't an array (aka. has one line)} else { // Put each piece of data as part of the array$csv_lines = explode($delimiter, $csv_lines);} // Return the array of datareturn $csv_lines; // End the function easy_csv} ?>

It produces an array like:

Array
(
[0] => Array
(
[0] => Australia
[1] => Malaysia
[2] => Bahrain
[3] => San Marino
[4] => Spain
[5] => Monaco
[6] => Europe
[7] => Canada
[8] => USA
[9] => France
)

[1] => Array
(
[0] => Britain
[1] => Germany
[2] => Hungary
[3] => Turkey
[4] => Italy
[5] => Belgium
[6] => Brazil
[7] => Japan
[8] => China
[9] => Total
)

)

MyDB

Tags: March 11, 2005 (0 comments)

This is the first real project I've started working on, although it is still primarily for myself, it doesn't mean I'll be compromising things for other people using it just for me. Although this is only version 0.8.1, it already has some features that I wouldn't of put in if I was doing it just for myself.

Anyhow, time for more detail, like what the hell it is. MyDB is a lightweight, yet powerful, database class. It grew out of part of Location Unknown, I was looking at various database engines, but there was always something wrong, I ended up deciding that I'd write my own. It took me around a day to plan and code the original MySQL version. After a couple of days of thinking about it, I decided to untie it from MySQL, adding support for other databases, such as PostgreSQL and SQLite. This lead to the class getting two extra functions, to keep one of the principals I had set myself when I decided to untie it from MySQL - Make sure as little as possible has to be changed when changing what database server you're using.

That pretty much tells you almost everything you need to know about it in one paragraph. But that doesn't mean I've got nothing more to say.

One of the principles I set out right in the very beginning was it would have very clear, commented code, as to make it easy for other people to build upon it, and hopefully release it to the web developer community. Modify it to your need, and if you're kind, release your code (in accordance with the license, of course).

Personally, I doubt I'll ever need anything more than MySQL, but I'm going to try and support PostgreSQL (anyone who can give me PostgreSQL hosting will be thanked) and SQLite, one of the main principles I set out when I decided to make it support more than one database server, was I'd make it so you have to change as little as possible, and that's likely to just be the connect function...

What licence am I going to release it under? I want this to be open source, yet I still want control over it, I decided to go with the Creative Commons Attribution-NonCommercial-ShareAlike 2.0 License. I'll explain my reasons for each part:

Attribution
I don't want this script to be slightly modified, and then be given no credit at all
Noncommercial
Many complex reasons, although, I'd like to say, anybody who wants to use it commercially, please do not hesitate to contact me
Share Alike
To stop people from changing the license to one that I count as inappropriate

Please, check it out, download it, or at very least visit it's website at http://mydb.geoffers.uni.cc/.

3, 2, 1, we have liftoff

Tags: February 19, 2005 (0 comments)

Countdown Scripts, we all need one somewhere, even if it just until our next birthday :P So, here's mine:

<?phpfunction countdown ($count) {$difference = $count - time();$abs_difference = abs($difference); $days = floor($abs_difference/60/60/24);$hours = floor(($abs_difference - $days*60*60*24)/60/60); $minutes = floor(($abs_difference - $days*60*60*24 - $hours*60*60)/60); if ($difference != $abs_difference) {return "It was $days days$hours hours and $minutes minutes ago!";} else {return "Only $days days$hours hours and $minutes minutes to go!";} }?>

To use it, you pass a *nix timestamp in the function call, eg. countdown (1108774476);
Remember you can also use the mktime function, eg. countdown (mktime (3, 20, 0, 4, 20, 1992));
Lastly, it returns the string, it doesn't output it, so to output it you'll need to use echo or print, eg. echo countdown (1108774867);

Page:  1 2 3