We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36805
    • 354 Posts
    Hi,

    I want to simplify linking to another non modx site of mine. It is always the same site and link format. The TinyMCE linklist is great but cannot be used because it in use for internal MODx links. Therefore I have written a snippet. It takes an id and makes an url out of it. Like this:
    [!link? &id=`240`!]
    

    would return
    <a href="http://www.externalsite.com/index.php?id=240">Read more here</a>

    This is rather unpleasant for content editors. It would be better if a plugin would just take something like:
    (%240%)

    When the page loads it would replace it with the link as the snippet does. Any ideas on how to code this would be very much appreciated by me.
      • 3749
      • 24,544 Posts
      You could do it with a plugin tied to OnBeforeDocFormSave.

      I don’t think you can use a standard modx tag ( [[ . . . ]]) because it will have been removed by then, but your example (%12%) should work.

      It would look something like this (untested):

      <?php
      /* ReplaceLinks plugin */
      /* Connect to OnBeforeDocFormSave */
      $pattern = "/\(%(.*)%\)/";
      $replacement = '<a href="http://www.externalsite.com/index.php?id=$1">Read more here</a>;
      
      $content = $resource->getContent()
      $newContent = preg_replace($pattern, $replacement, $content);
      $resource->setContent($newContent);
      
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36805
        • 354 Posts
        Very nice! Thanks! It works for me after some adjustments.

        (I’ve added quotes and escapes. Also altered it to make it run on Evolution. Sorry for posting to wrong board)

        /* ReplaceLinks plugin */
        /* Connect to OnBeforeDocFormSave */
        
        global $content;
        
        $pattern = "/\(%(.*)%\)/";
        $replacement = "<a href=\"index.php?id=$1\">Read more here</a>";
        
        $content = preg_replace($pattern, $replacement, $content);