Plugin: Text Replace

This plugin has been updated! Comments to this post are now closed. For the latest download, documentation, and link for posting new comments related to this plugin, visit the plugin’s new homepage at:
coffee2code.com/wp-plugins/text-replace
Name:
Text Replace
Author:
Scott Reilly
Version:
1.0
Last updated:
27 March 2005
Description:
Replace text with other text in posts, etc. Very handy to create shortcuts to commonly-typed and/or lengthy text/HTML, or for smilies.

Notes:

SPECIAL NOTE FOR UPGRADERS:
– Remember to copy the text replacement array ($text_to_replace) from your current text-replace.php before overwriting it with the latest version. You’ll need to transfer that information into the newer version. (And prior to all that, just go ahead and make a backup of the text-replace.php you are currently using prior to any upgrade of the plugin.

This plugin can be utilized to make shortcuts for frequently typed text, but keep these things in mind:

  • Your best bet with defining shortcuts is to define something that would never otherwise appear in your text. For instance, bookend the shortcut with colons:

    “:wp:” => “<a href=’http://wordpress.org’>WordPress</a>”,
    “:aol:” => “<a href=’http://www.aol.com’>America Online, Inc.</a>”,

    Otherwise, you risk proper but undesired replacements:

    “Hi” => “Hello”,

    Would have the effect of changing “His majesty” to “Hellos majesty”. One solution might be to instead have used ” Hi ” (with spaces).

  • Do not use the backspace character, ‘\’, within the shortcut text.
  • Be mindful of your use of quotes as you define replacement text. Escape embedded double-quotes.
  • List the more specific matches early, to avoid stomping on another of your shortcuts. For example, if you have both “:p” and “:pout:”
    as shortcuts, put “:pout:” first, otherwise, the “:p” will match against all the “:pout:” in your text.
  • If you intend to use this plugin to handle smilies, you should disable WordPress’s default smilie handler.
  • This plugin is set to filter the_content, the_excerpt, and if uncommented, comment_text.
  • Movable Type users: This plugin could be used to do what MT’s <MTMacroDefine> did:

    <MTMacroDefine name="cool" string="8)"> <img src="http://www.yoursite.com/images/cool.gif" alt="8)" /> </MTMacroDefine>

    Now becomes:

    "8)" => "<img src='http://www.yoursite.com/images/cool.gif' alt='8)' />"

  • SPECIAL CONSIDERATION: Be aware that the shortcut text that you use in your posts will be stored that way in the database (naturally). While calls to display the posts will see the filtered, text replaced version, anything that operates directly on the database will not see the expanded replacement text. So if you only ever referred to “America Online” as “:aol:” (where “:aol:” => “<a href=’http://www.aol.com’>America Online</a>”), visitors to your site will see the linked, expanded text due to the text replace, but a database search would never turn up a match for “America Online”.
  • However, a benefit of the replacement text not being saved to the database and instead evaluated when the data is being loaded into a web page is that if the replacement text is modified, all pages making use of the shortcut will henceforth use the updated replacement text.
Installation:
  1. Download the file text-replace.zip and unzip it into your wp-content/plugins/ directory.
    -OR-
    Copy and paste the the code ( text-replace.phps ) into a file called text-replace.php, and put that file into your wp-content/plugins/ directory.
  2. Modify the array $text_to_replace in the plugin file to define the text replacements you’d like to have happen.
    Note: If upgrading, you’ll want to copy this information from your previous version of this plugin.
  3. Activate the plugin from your WordPress admin ‘Plugins’ page.
  4. Use the shortcut in a post.
Functions:

function c2c_text_replace( $text, $case_sensitive=false )

  • $text : The text to processed for text replacement.
  • $case_sensitive : Boolean value (either ‘true’ or ‘false’) to indicate if the search for shortcut text for possible replacement should be case sensitive or not; default is ‘false’
Tips & Examples:

N/A — Just type your shortcut text into a post and when viewed on your site you should see the expanded text.

Related Links:
Release Log:
  • 27 Mar 2005 : v1.0 released —
    • Moved the array $text_to_replace outside of the function and into global space
    • Renamed function from text_replace() to c2c_text_replace()
    • Changed license from BSD-New to MIT
    • Added installation instruction and notes to plugin file
    • Verified that plugin works for WordPress v1.2+ and v1.5+
  • 23 Sep 2004 : v0.92 released — added optional argument $case_sensitive (defaulted to ‘false’); changed from BSD-new to MIT license
  • 09 Jul 2004 : v0.91 released, see comments
  • 29 Jun 2004: v0.9 released to the public
Copyright & Disclaimer:

Copyright (c) 2004-2005 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements:
I’d like to thank Joel Bennett for his Acronym Replacer plugin, which I shamelessly modified.

68 thoughts on “Plugin: Text Replace

  1. Any idea if text-replace can work with feedwordpress plugin? The text-replace plugin works great for ‘normal’ posts however it does not affect posts created by feedwordpress from a syndicated feed.

  2. Im trying to use this to link to an affiliate code by replacing a specific word with a link such as this:

    Any idea why this type of link isnt working?

    I have the run-php and exec-php plugins installed and, the links work fine when manually added to a post just not when using text-replace :(

    Any help you could offer would really be appreciated :)

  3. 
    [?php $query=getenv(\"QUERY_STRING\");  
    if ($query!=NULL) 
    echo 'http://refer.ccbill.com/cgi-bin/clicks.cgi?CA=929078-0000&amp;PA='.$query.'&amp;HTML=http://www.bisexualcocks.com'; 
    else echo 'http://refer.ccbill.com/cgi-bin/clicks.cgi?CA=929078-0000&amp;PA=1095270&amp;HTML=http://www.bisexualcocks.com'; 
    ?]

    Trying to post the code again.

  4. Scott, you say in two sequential tips:

    * Do not use the backspace character, ‘\’, within the shortcut text.
    * Escape embedded double-quotes.

    How do I escape embedded double-quotes?

    Thanks

Comments are closed.