We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3071
    • 99 Posts
    Not sure why that is happening, I have actually managed to make it only interlink the first keyword it sees now thanks to the genius that is MrHaw! I now need to work out how to set the number of keywords in that variable so I can change at will. Next mission is to also work out how to make it look for 3 keywords per page.

    This is the code I am using in the actual plugin:

    $output = &$modx->documentOutput;
    $output = replace_first(" madonna ", '<a href="madonna.html"> madonna </a>', $output);
    return $output;


    I also added this to the end of my manager/includes/document.parser.class.inc.php document:

    function replace_first($search, $replace, $data) {
        $res = strpos($data, $search);
        if($res === false) {
            return $data;
        } else {
            // There is data to be replaced
            $left_seg = substr($data, 0, strpos($data, $search));
            $right_seg = substr($data, (strpos($data, $search) + strlen($search)));
            return $left_seg . $replace . $right_seg;
        }
    }
    


    The Wordpress one works like a dream, I am sure we can create the same thing for MODX!
      • 20413
      • 2,877 Posts
      You could put the function in the plugin! smiley

      see http://modxcms.com/forums/index.php/topic,53750.msg310798.html#msg310860

      @kirkhuyser Does The Revo API use documentOutput?
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 3071
        • 99 Posts
        I messed around with some variations then placed it at the end of that document and it worked, will probably move it though now, thanks! Just trying to work out the other stuff now.
          • 3071
          • 99 Posts
          More issues, just looked at the source code on the rendered page. For some reason it had placed 9 sets of the link in, despite these not being visibile on the rendered page itself:

          <a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"><a href="http://www.example.com/madonna"> madonna </a></a></a></a></a></a></a></a></a>


          Just when I think I am getting somewhere...:(

            • 20413
            • 2,877 Posts
            As I’m sitting on a public computer (with caching issues and a retarded firewall) I can’t test any code. I’ll look into that function later! smiley
            //Restore your document.parser.class.inc.php and if you get MODx errors let me know about them!
              @hawproductions | http://mrhaw.com/

              Infograph: MODX Advanced Install in 7 steps:
              http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

              Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
              http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
              • 3071
              • 99 Posts
              Cool. Will be good to hear your thoughts!! Just checked another site and it indeed place 9 instances of the href link in the code..Wonder why..
                • 15953
                • 123 Posts
                @kirkhuyser Does The Revo API use documentOutput?

                Yes:
                "Event: OnParseDocument
                Fires on each time the Element tags are parsed. This can happen many times during the loading of a Resource. To reference the content of the Resource, use $modx->documentOutput."

                I saw a blank page because I refreshed my current page. When browsing the page anew here’s the error:

                "This error (HTTP 500 Internal Server Error) means that the website you are visiting had a server problem which prevented the webpage from displaying."
                  • 20413
                  • 2,877 Posts
                  Ok home and testing! smiley

                  This is how we have to do it:
                  if (!function_exists('str_replace_once')) {
                      function str_replace_once($needle , $replace , $haystack){
                          // Looks for the first occurence of $needle in $haystack
                          // and replaces it with $replace.
                          $pos = strpos($haystack, $needle);
                          if ($pos === false) {
                              // Nothing found
                              return $haystack;
                          }
                          return substr_replace($haystack, $replace, $pos, strlen($needle));
                      }
                  } 
                  $output = &$modx->documentOutput;
                  $output = str_replace_once(" maddona ", ' <a href="maddona.html">maddona</a> ', $output); //notice str_replace_once
                  $output = str_replace_once(" RedBull ", ' <a href="redbull.html">RedBull</a> ', $output); //instead of str_replace
                  return $output;
                  
                    @hawproductions | http://mrhaw.com/

                    Infograph: MODX Advanced Install in 7 steps:
                    http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                    Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                    http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                    • 3071
                    • 99 Posts
                    Ok! But what does this bad boy do that the other doesnt? I am trying now!!
                      • 20413
                      • 2,877 Posts
                      Yes I used the other function (that does the same thing)
                      The important thing here was to do:
                      if (!function_exists('myFunction')) {
                      function myFunction()...
                      }
                      
                        @hawproductions | http://mrhaw.com/

                        Infograph: MODX Advanced Install in 7 steps:
                        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower