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. great plugin – thanks! one thing you may want to know is that using square brackets around a word (ie. [person]) will cause it to break. I was using a much older b2 version that linked names in the brackets and just copied my array over. I’m glad it’s easy to change to another character (like the colon) thanks to search and replace in phpMyAdmin 🙂

  2. Thanks, Kristal! If you wanted to maintain your usage of the square brackets, in the remapping array you could define them like:
    “[person]” => “<a href=’http://www.personsite.org’>Joe Person</a>”,

    Then in your posts you could continue to type “[person]” to get the text replacement.

    I really ought to list the characters that need escaping.

  3. This is a grrreat plug-in, but is there any way to get it to ignore the text in a href tags (either at my end or at yours)? I’m having problems with a hrefs being embedded inside a hrefs.

  4. Is the unwanted replacement taking place within the < and > part of the tag (i.e. <a href=”http://xxxUNWANTEDyyy”>text</a>) or in the text encapsulated between the start/end tags (i.e. <a href=”http://xxxyyy”>textUNWANTED</a>)? If the former, I’m currently working on update to this plugin which, among other things, will not replace text within a tag.

  5. Scott, I believe it was the former, but I’ve been so busy the last few days I really don’t even remember. Nevertheless I look forward to the update!

  6. PLUGIN UPDATED: v0.91

    I just updated this plugin. This release:

    – Removes the need to escape special characters used in the shortcut text. Now “?”, “(“, “)”, “[“, “]”, etc can be used without problems. However, the backspace character “\” should be avoided.

    – Employs a new pattern for matching and replacing text. A huge advantage of this new matching pattern is that it won’t match text in a tag (text appearing between ‘<‘ and ‘>”).

    So if you had defined a smilie
    ":/" => "&lt;img alt=':/' src='/wp-images/smilies/slant.gif' />",
    It will no longer turn this:
    <a href=”http://www.yyy.com”>site</a>
    -into-
    <a href=”http<img alt=’:)’ src=’/wp-images/smilies/slant.gif’ />/www.yyy.com”>site</a>
    (However, if you had a plaintext http:// string it would get its “:/” replaced, so you’re really better off trying not to use “:/”.)

    Given this new pattern matching expression, let me know if it matches/doesn’t match for replacement when it should

  7. Would it not be possible to get the plugin to ignore everything between ‘<a’ and ‘</a>’ using a series of if and or statements? I don’t really know too much PHP, but perhaps you’re already working on something like this.

  8. Ben, please provide an example of the shortcut, its expansion, and the text you are using in your post that you don’t want to get a replacement. v0.91 (you are using the latest, aren’t you?) is supposed to ignore any text defined within a tag, but it may need some tweaking.

  9. The shortcut in this case is:
    “Toronto” = > ” < a href=’http://www.city.toronto.on.ca/’ > Toronto </a >”

    And it messes up the following link:
    <a href=”SOMELINK” > Toronto Wins Baseball Game </a >

    It replaces the Toronto with:
    < a href=’http://www.city.toronto.on.ca/’ > Toronto </a >

    Producing:
    <a href=”SOMELINK” > < a href=’http://www.city.toronto.on.ca/’ > Toronto </a > Wins Baseball Game </a >

  10. Ah, now I see what you’re talking about. I’ll give it some thought and see about keeping it from performing a replacement if said replacement would put linked text within text already linked. No guarentees, though.

  11. …Any way to make this work for the_author?
    I tried adding add_filter(‘the_author’, ‘text_replace’, 2);
    Doesn’t seem to be working.
    Thanks very much.

  12. Ryan,

    The 2 in add_filter() is used to set the priorty for the filter (i.e. this plugin). 2 indicates that I want this plugin to operate on the text earlier rather than later, before other filters have a chance to look at it.

    Adding the filter you specified should theoretically work. However, the pattern matching code may not be geared toward matching if the string it was sent is the thing you want to be replaced. Might be able to handle that with minimal change, though. I’ll have to take a look at it.

    If you feel like testing, modify the text_replace() function as such:

    1.) Add this as the first line:

    $text = " " . $text . " ";

    2.) Change the last line to this:

    return substr($text,1,strlen($text)-2);

  13. Does this work automatically, or does someone have to run the plugin?

    What I want to use it for is to change all instances of the F-bomb, for example, to something lame, like “fudge”. Will it work for this purpose?

    Thanks,
    Thomas

  14. Plugin updated to v0.92

    The update features:

    • The addition of the optional argument $case_sensitive. The defaut value is ‘false’. It indicates if the search for shortcut text replacement should be case sensitive or not.
    • Change from BSD-new license to MIT license
  15. Thomas: Yes, this works automatically, once you’ve installed and activated the plugin. And you should be able to change all instances of the “f-bomb”. In the code, you have to define the replacement. Find where the $text_to_replace array is being set, you’ll see this example line:

    ":wp:" => "<a href='http://wordpress.org'>WordPress</a>",

    Add something like:

    "fbomb" => "fudge",

    That should do it.

  16. Hey whatsup.
    I run the Recent Comments plugin by MtDewVirus.
    It doesnt seem to replace the text of the recent comments….
    ?because its a php script possibly?
    Is there any way to get these two working together?
    Lemme know.
    Check out my site www.flasurfers.com

  17. i just read above…
    This plugin is set to filter the_content, the_excerpt, and if uncommented, comment_text.

    How can i activate it for the content ? or right side. or the recentcomments.php

  18. I’ve just downloaded this plugin, and I’m trying to figure out how you actually define the text that you want shortcutted? for example, I would like to define :sproke: to go to a webpage www.ordinarylife.co.nz/about (or some address like that) but I don’t know how to define the text to do this. I would like to use the plugin so that the people I referr most in my coloums can be linked to a ‘character sheet’ if you will.

    Cheers for your help
    Sproke :0)

  19. James: MtDewVirus’s Recent Comments plugin does not apply filters to the comments, therefore the comments that plugin shows don’t get processed by other plugins. To rectify that, replace this line in the Recent Comments plugin file:

    $comment_content = strip_tags($comment->comment_content);

    with this one:

    $comment_content = strip_tags(apply_filters('comment_text', $comment->comment_content));

    Mind you, I haven’t tested that at all.

    Sproke: At the beginning of the text_replace function, you’ll see where an array called $text_to_replace is defined. You’ll see a sample text replacement defined within that, like so:

    ":wp:" => "<a href='http://wordpress.org'>WordPress</a>",

    All you need to do is add another line after that. For instance, you wanted this:

    ":sproke:" => "<a href='http://www.ordinarylife.co.nz/about'>Sproke</a>",

    That way if you type :sproke:, it’ll get replaced with the word “Sproke” linked to that ‘about’ page.

  20. Whi doesn’t it work with me? My code is like this:

    
    $text_to_replace = array(
    ":wp:" => "|<a href='http://wordpress.org'>WordPress</a>|",
    ":wpwiki:" => "|<a href='http://wiki.wordpress.org'>WordPress Wiki</a>|",
    ":friend1:" => "|<a href='http://domain.com'>friend1</a>|",
    ":friend2:" => "|<a href='http://domain.com>friend2</a>|",
      );
    

    It works with the first two links, but not with the others. I really don’t understand why…

  21. I wrapped the code you pasted in <code>< /code> tags for better viewing. One problem I see is that for the :friend2: link, you omitted the closing single-quote for the href. Not sure if that is the source of the problems, but I’d fix that first and see if the problem persists.

  22. Ah, thank you very much 🙂
    I just copied wrong, but in the original file there are all the quotes and single-quotes. That’s why I’m confused @_ò

  23. Plugin updated to v1.0

    Updates in this release:
    * 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+

    Note to upgraders: Be sure to save a copy of your $text_to_replace array from your current version of the plugin so that you may include it in the newer version.

  24. Can I use this plugin to change text into the code needed to display it as an acronym? I’ve tried adding these to it but I keep getting the following error:

    Parse error: parse error, unexpected T_STRING, expecting ‘)’ in /home/theldag/public_html/shade/wp-content/plugins/text-replace.php on line 89

    Thanks in advance for your help.

  25. Any idea what would cause the following problem in the admin panel (all pages):

    Warning: Cannot modify header information – headers already sent by (output started at /home/xxxx/www/xxxx/wordpress/wp-content/plugins/text-replace.php:117) in /home/xxxx/www/xxxx/wordpress/wp-admin/admin.php on line 6

    Goes through lines 6 – 9 and 21. If I deactive the plugin, the admin panel is normal. My site is running WP 1.5 with DashLite.

  26. Pingback: Erik Johansson
  27. Pingback: Erik Johansson
  28. Hi – great plugin. I’ve been using it to replace text I already have on my site and it works like a dream. The only problem is – how can I replace text which has apostrophies in? The plugin doesn’t work for text such as “Jess’s site”.

  29. Hi! I am using the bilingual plugin and it uses a field to enter the excerpt in another language and then displays it in the post… The text replace works for the post but not for the excerpt… Is there any way to make it work for the excerpt too? Which like I said is only an extra field and should be part of the content…

  30. Can you help? My goal is to bold every appearance of the phrases SET ONE:, SET TWO:, and ENCORE:. Text-Replace works as expected only on some of my posts. It seems to be working on new/future posts, but not working on old posts I back-dated with WordPress “Edit Timestamp” or edited after publishing.

    I tried re- Activating the plug-in and several variations on the text-replace array. I was concerned that having a space between the words SET ONE, and SET TWO, was the problem, but couldn’t get just TWO: to replace with (bold) TWO: either. Here’s what I’m trying:

    
    $text_to_replace = array(
      ":wp:" =&gt; "<a href='http://wordpress.org' rel="nofollow">WordPress</a>",
      "SET ONE:" =&gt; "<strong>SET ONE:</strong></a>",
      "SET TWO" =&gt; "<strong>SET TWO:</strong>",
      "ENCORE:" =&gt; "<strong>ENCORE:</strong>",
    );

  31. Hi. Great stuff, but what about something like this:

    A plug-in that matches only whole words, and replaces them with links. This way, a user would create an array like this:

    "WordPress" =&gt; "http://wordpress.org",
    "WordPress Wiki" =&gt; "http://wiki.wordpress.org",

    And then in the actual post, wherever WordPress occurs as a whole word, it would appear as a link!

Comments are closed.