- b2evolution CMS Support Forums
- b2evolution Support
- Plugins & Extensions
- [PLUGIN] Search & Replace Plug-in
1 kwa Oct 28, 2005 13:01
What does this plugin do?
[list]This Search & Replace Plug-in intends to search for a given strings into a displayed post's content and replace them with other strings. It's a common Search & Replace mechanism applied to all the displayed [url=http://b2evolution.net]b2evolution[/url] posts.[/list:u]
What b2evolution versions are supported?[list]The following implementation supports most b2evolution versions, including:[list]
0.9.0.x (Europe, Berlin, Oslo, Madrid, Copenhagen, Paris, Amsterdam),
0.9.1 (Dawn).[/list:u]
Compatibility with the yet to be release 0.9.2 (Phoenix) release has not been tested (yet).[/list:u]
When should I use it?
[list]This plug-in has been developed to replace all the full URLs referencing an online blog to the local paths.
In my case, I wanted to replace all the occurrences of my online site URLs:
blog.lesperlesduchat.com/
to the local ones:
localhost/lesperlesduchat/
in my local development folder. In that way, I was able to click on my posts' links without leaving my local blog or displaying local images instead of requiring Internet connexion still active.
The same plug-in can also be used to fix misspelled words, fixing e-mail addresses (using obfuscated addresses for example) or anything else you want to replace to.[/list:u]
What are the plugin limitations?
[list]The current version does not support regular expressions in the search and replace strings. However, that might be added in a future version. Since regular expressions are slower to process than simple strings, it might be interesting to use the standard text when regular expressions are not required.
There is also a lack of user interface. The plug-in configuration is global to all blogs and has to be done in the plugin source code itself.[/list:u]
Implementation
[list]Create a _searchandreplace.renderer.php file into your plugins/renderers folder of your [url=http://b2evolution.net]install[/url] and copy-paste the following code:
<?php
/**
* This file implements the Search & Replace Plug-in 0.1
* intended for use with b2evolution 0.9.0.x and 0.9.1.
*
* This plug-in is intended to search and replace given text strings.
*
* Released under GNU GPL License - {@link http://www.gnu.org/copyleft/gpl.html}
* @copyright (c)2005 by kwa - {http://blog.lesperlesduchat.com/dev.php}
*
* @package plugins
*/
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
/**
* Includes:
*/
require_once dirname(__FILE__).'/../renderer.class.php';
/**
* @package plugins
*/
class SearchAndReplace_RendererPlugin extends RendererPlugin
{
var $code = 'b2eSnR';
var $name = 'Search & Replace 0.1';
var $priority = 100;
var $apply_when = 'stealth';
var $apply_to_html = true;
var $apply_to_xml = true;
var $short_desc;
var $long_desc;
//>>> START of plugin-specific configuration variables ********************
var $SearchAndReplace = array(
array( 'search' => 'your search string', 'your replacement string' => 'localhost/lesperlesduchat/' ),
);
//<<< END of plugin-specific configuration variables **********************
/**
* Constructor
*
* {@internal SearchAndReplace_Rendererplugin::SearchAndReplace_Rendererplugin(-)}
*/
function SearchAndReplace_Rendererplugin()
{
$this->short_desc = T_( 'Search and replace strings into your posts.' );
$this->long_desc = T_( 'This plug-in searches for strings in your posts and replaces it with other strings. This is a regular Search & Replace feature applied to all your blogs' posts. (This plug-in is intended for b2evolution versions 0.9.0.x and 0.9.1.)' );
}
/**
* Perform rendering
*
* {@internal SearchAndReplace_Rendererplugin::render(-)}
*
* @param string Content to render (by reference) / rendered content (IGNORED).
* @param string Output format, see {@link format_to_output()} (IGNORED).
* @
return boolean Always true.
*/
function render( & $content, $format )
{
foreach( $this->SearchAndReplace as $SearchAndReplace )
{
$content = str_replace( $SearchAndReplace['search'], $SearchAndReplace['replace'], $content );
}
return true;
}
}
// Register the plugin
$this->register( new SearchAndReplace_RendererPlugin() );
?>
[/list:u]
Configuration
[list]In the code above, edit the following lines:
var $SearchAndReplace = array(
array( 'search' => 'your search string', 'replace' => 'your replacement string' => 'localhost/lesperlesduchat/' ),
);
to all the search and replacement strings you want to. An example:
var $SearchAndReplace = array(
array( 'search' => 'mail@isp.com',
'replace' =>
'mail@isp.com' ),
// HTML entities encoded/obfuscated mail in order to make mail grabbing more difficult for spammers
array( 'search' => 'corect', 'replace' => 'correct' ), // misspelled word
array( 'search' => 'WordPress rules', 'WordPress rules, but b2evolution rules more' ), // misspelled word
);
Once saved, the plugin is automatically taken into account and applied to all the displayed posts.[/list:u]
I've just fixed the plug-in description and a documentation comment. The plug-in features and other code have still unmodified.