Forums

How to replace text (regex?) in all posts, or define custom macros (3 posts)

  1. Reuben
    Member
    Posted 2 years ago #

    Hi,

    I used to use a plugin called smart link (by Semiologic.) It's now part of a commercial product, and the old version is no longer supported. And, the old version no longer works with WordPress 2.8.2, so all of my old links have died - i.e. are rendered in the page as [foo -> bah].

    I'm looking for one of two solutions:
    a) a plugin that lets me define a macro that will behave as a link with the syntax above
    b) the ability to find/replace using regex in all of my posts

    Ideas?

    Thanks

  2. Dagon Design
    Member
    Posted 2 years ago #

    You can replace text dynamically with a plugin. Here is a basic example:

    <?php
    /*
    Plugin Name: Replace text
    Plugin URI:
    Description:
    Author:
    Version: 1.0
    Author URI:
    */
    
    function replace_the_text($content) {
    	$content = str_replace("foo", "bah", $content);
    	return $content;
    }
    
    add_filter('the_content', 'replace_the_text');
    
    ?>

    As you can see, the 'replace_the_text' function gets the content of the post/page, modifies it, and returns it back where it is displayed.

  3. Reuben
    Member
    Posted 2 years ago #

    Thanks! Here's what I ended up with. The regex possibly needs a little tweaking, but it works on everything I've checked so far. I'm going to post this on the plugin repo in case anybody else needs it.

    <?php
    /*
    Plugin Name: Replace text
    Plugin URI: http://replacetext.com
    Description: Replaces text
    Author: Reuben Firmin
    Version: 1.0
    Author URI: http://www.flavor8.com
    */
    
    function replace_the_text($content) {
            $content = ereg_replace("\[([0-9a-zA-Z\(\)\'\" ]*)[ ]*\-\>[ ]*([0-9a-zA-Z:/\.\_\-\'\(\)]*)\]", '<a href="\2">\1</a>', $content);
            return $content;
    }
    
    add_filter('the_content', 'replace_the_text');
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.