• I am attempting to write a filter that extracts part of the the_content and uses it in another function to return a formated string (a link).

    In short, every time a user puts {{id:999}} in the post body (where 999 is any integer), I want it to return a formated link created by another function ( tnk_contact_link() ).

    Here is my non-working example:

    tnk_contact_filter($text);
    {
    
        $pattern = '\{id\:[0-9]+\}';
        $stat = preg_match_all($patern, $text, $hit);
    
        foreach($hit as $contact)
        {
            $id = str_replace(array('{{id:', '}}'), '', $contact);
            $replacement = tnk_contact_link($id);
            $filtered = str_replace($contact, $replacement, $filtered);
        }
        return $filtered;
    
    }
    
    add_filter('the_content', 'tnk_contact_link');

    Right now it causes the post to disappear because it is not doing the find/replace inline.

    All I really need is a plugin that does something similar to look at, and I think I can figure it out from there.

Viewing 1 replies (of 1 total)
  • Thread Starter yearginsm

    (@yearginsm)

    Here is the version that eventually took shape. It replaces against a lookup for {c:999}. I’m posting it here for anyone who might come across it.

    function tnk_contact_filter($text)
    {
        preg_match_all('/\{c:([0-9]+)\}/', $text, $brother);
    
        foreach ($brother[1] as $id)
        {
            $find[] = "/\{c:($id)\}/";
            $replace[] = tnk_contact_link($id);
        }
    
        if (!empty($id))
        {
            $text = preg_replace($find, $replace, $text);
        }
    
        return $text;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Writing a two-step filter plugin’ is closed to new replies.