Dynamic Text Replace

Author:Scott Reilly
Version:2.0
Last update:09 April 2008
Compatibility:WP 2.2+, 2.3+ and 2.5+
Comments:go here
Download:[ zip ]
Description:

Define your own custom dynamic text replacement pseudo-functions (i.e. ::google(“search terms here”)::)

Extended Description

Some notes on the plugin:

  • Read later in this page to learn how you can customize the text sent to the pseudo-functions (and see examples at the end of the plugin file).
  • This plugin filters the_content and the_excerpt.
  • SPECIAL CONSIDERATION: Be aware that the dynamic 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 have
    ::google("green cats"):: (which becomes <a href='http://www.google.com/search?q=green+cats'>green cats</a>), visitors to your site will see the linked, expanded text due to the text replace, but a database search would not turn up the post as a match for “google.com“.
  • 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 dynamic replacement text is modified, all pages making use of the shortcut will henceforth use the updated replacement text.

Screenshots

  • A screenshot of the admin options page for the plugin
    screenshot 1

Installation

  1. Download the file dynamic-text-replace.zip and unzip it into your wp-content/plugins/ directory.
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress.
  3. Go to the Options -> Dynamic Text Replace (or for WP 2.5: Settings -> Dynamic Text Replace) admin options page. Optionally customize the options (notably to define the dynamic shortcuts and their replacements).
  4. Use the dynamic shortcuts in a post.
  5. (optional) If you need to alter the text sent to one of the pseudo-functions you’ve created, you can filter it by adding a filter on ‘dynamic_text_replace_$pseudofunction` where $pseudofunction is the name of the pseudo-function you’ve created. See the Frequently Asked Questions section for an example.

Frequently Asked Questions

  • Q: Does this plugin modify the post content in the database?

    A: No. The plugin filters post content on-the-fly.

  • Q: Will this work for posts I wrote prior to installing this plugin?

    A: Yes, if they include strings that you’ve now defined as shortcuts.

  • Q: What post fields get handled by this plugin?

    A: The plugin filters the post content and post excerpt fields, and optionally comments and comment excerpts.

  • Q: Is the plugin case sensitive?

    A: Yes, as far as the pseudo-function names go.

  • Q: What happens if I use a dynamic text replacement pseudo-function that doesn’t exist?

    A: Nothing will be displayed in its place, nor will the pseudo-function itself appear.

  • Q: How do I define my own %text_altered% value for a pseudo-function I’ve created?

    A: Assume you created this dynamic text replacement pseudo-function:

    yahoo => <a href="http://search.yahoo.com/search?p=%text_altered%" title="Search Yahoo! for %text%">%display%</a>

    Why do you even need to use %text_altered% in the first place? Because you need to alter the text provided to the pseudo-function for use in the replacement string. In this case, you expect to enter this shortcut in your post, ::yahoo(“green cats”)::, however, the Yahoo! search string in the URL doesn’t like spaces and needs for the search string to be defined as “green+cats”. So you need to add a custom filter on the text, like so:

    add_filter('dynamic_text_replace_yahoo', 'dtr_space_to_plus');

    The filter name is always “dynamice_text_replace_” followed by the pseudo-function name. The second argument is the function to call, which will be sent the original text value supplied as the first argument to your pseudo-function. The value that that function returns will be treated as the %text_altered% value. This example is using a handy helper function provided by this plugin to convert spaces to plus signs, but you can call any function you want.

    As a result of the above, the output is:

    <a href="http://search.yahoo.com/search?p=green+cats" title="Search Yahoo! for green cats">green cats</a>

    Additionally, you can filter the %display% value:

    
    function yahoo_display($display_text) {
            return "Yahoo! for " . strtoupper($display_text);
    }
    add_filter('dynamic_text_replace_display_yahoo', 'yahoo_display');
    

    Which would yield:

    <a href="http://search.yahoo.com/search?p=green+cats" title="Search Yahoo! for green cats">Yahoo! for GREEN CATS</a>


Release Log


Copyright & Disclaimer

Copyright © 2005-2008 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

Thanks to all those who have contributed feedback and support!