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

    I’m trying to make my first plugin, and I’m really not php dev, so I sweat a little bit...

    Goal is to manage automatically external links to add a wanted class (the string is contained into $extlink_class) + a rel="external" statement to each of them. (in fine, the goal is to keep strict validity without ’target_blank links’, some JS will do the final job).

    I made something working, but it’s too basic and not idiot proof, because in the RTE, the redactor could have already added the external class manually to a link, AND a link could have already some classes defined (my first version crusches the class statement).

    What could be the preg_replace good expression to do the following :

    The motif would be like this : in the dom look for each link, string beginning by ’<a’ followed by anything til the final ’>’
    Look inside for a string beginning by ’class="’ huh
    Taking out inside this one the content (string) of $extlink_class (in cas it is already set)
    them add it (again if it was here before, yes) to the previous class statement (keeping potential other already set classes)

    I think about this as a beginning :
    $motif = '<a[^>]+)>';


    I’d like to use this :
    $modx->documentOutput = preg_replace($the_motif,$the_replacement,$modx->documentOutput);


    and here’s what I’ve done in the first basic version:
    /*
    Plugin: Auto-extlink , set OnWebpagePrerender
    Description: Auto-extlink get all external links (http:// scheme) and add a rel="external" + class="ext-link" in your MODx content to all external links.
    Version: 0.2
    Author: Eric Lacomblez
    
    */
    
    
    $extlink_class = 'lien-externe';
    
        $text = $modx->documentOutput;
        // search and replace external Links in document    
                $modx->documentOutput = str_replace('base href="http://','basehref="http://',$modx->documentOutput);
                
                  $modx->documentOutput = str_replace(' href="http://',' class="'.$extlink_class.'" rel="external" href="http://',$modx->documentOutput);
            
              $modx->documentOutput = str_replace('basehref="http://','base href="http://',$modx->documentOutput);
    
            return; // stop  
    


    thank you
      Schtroumpf Grognon - Grouchy Smurf
      ---------------------------------
      Faites pas attention.. - Don&#39;t pay attention
      http://www.dzi-neo.net