How to make an Wordpress plugin from scratch

Filed Under (php, wordpress) by The Chef on 03-07-2009

Tagged Under : , , ,

<?php
/*
Plugin Name: My-plugin
Plugin URI: http://ezbitz.com/
Description: Remove all links from comments
Version: 1.0
Author: The Chef
Author URI: http://ezbitz.com
*/

function clean_comments($text)
{
	return preg_replace ( '%<a[^>]*>(.*?)</a>%si', '$1', preg_replace ( '/\b(https?)(:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i', 'hxxp$2', $text ) );
}

add_filter ( 'comment_text', 'clean_comments' );

?>

Create an empty php file. First of all we need to fill some plugin details that will appear on the plugins page: plugin name, description, author, version etc.
For this example I’ve decided to make a small plugin that removes the links from comments. For this, we need to hook the wordpress function that displays comments from the database. For an in-depth analisys of a plugin writing, go to http://codex.wordpress.org/Writing_a_Plugin. Let’s go back to our hooks: the hook that deals with the comments retrival is “comment_text”, so we set the hook on this function. In other words, after the comment content is retrieved from the database and before it is sent to browser, our function is called, here

function clean_comments($text)

Inside this function we use 2 cascaded regex in order to remove link tag and replace http with hxxp. This take only one line of code. I know this isn’t so elegant but for the artistic effect it works:) .
And that’s our plugin.
Make a directory inside wp-content/plugins and copy php file there. Go to plugin administration panel and whatch your plugin there !

10 tools a C/C++/C#/PHP programmer should never miss

Filed Under (c#, c/c++, mysql, net, php, system, tools) by The Chef on 13-05-2009

Tagged Under : , , ,

PHP: Simple upload image url to ImageShack

Filed Under (php) by The Chef on 26-04-2009

Tagged Under : , , ,

Here is a function you can use to directly upload url images to ImageShack. The function returns an associative array with 2 elements representing the ImageShack url of the image and of the thumbnail


function uploadURLToImageshack($url)
{
$ch = curl_init ( "http://www.imageshack.us/transload.php" );

$post ['xml'] = 'yes';
$post ['url'] = $url;

curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Expect: ' ) );

$result = curl_exec ( $ch );
curl_close ( $ch );

if (strpos ( $result, '<' . '?xml version="1.0" encoding="iso-8859-1"?>' ) === false)
{
return NULL;
} else
{
$xml = new SimpleXMLElement ( $result );
return array (
"image" => $xml->image_link,
"thumb" => $xml->thumb_link );
}
}