We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6511
    • 60 Posts
    danny_kay1710 Reply #41, 16 years ago
    </php
    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;
    $replacementText = ' <a href="machinery-safety">machinery safety</a>';
    
    if(strpos($output, $replacementText) === false)
      $modx->documentOutput = str_replace_once(' machinery safety', $replacementText, $output);
    ?>
    


    From my testing the above works - but it left some artefacts on my menu since on my page the keyword is in the menu.
      • 3071
      • 99 Posts
      Hey, I don’t want to speak too soon...but this seems to work and not leave 9 hrefs in the source code. What exactly did you change there, I have been trying variations for hours! Thanks very much!
        • 6511
        • 60 Posts
        danny_kay1710 Reply #43, 16 years ago
        Scratch that - that code actually replaces the first instance of the keyword in every snippet/chunk/document on your page.

        So my menu is a chuck so it replaces it in my menu.
        My content is a document so the first instance was replaced.
        I have a chuck for a contact box on the left hand side and code replaced the first instance of the keyword in that as well.

        The only workaround I can think of is on your document which is what you want this code to run on add a comment such as <!-- Begin Content -->
        Search for this and if found do the code else don’t bother

        <?php
        $output = $modx->documentOutput;
          
        if(strpos($output, '<!-- Begin Content -->') !== false)
        {
          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));
              }
          }
        
        
          $replacementText = ' <a href="machinery-safety">machinery safety</a>';
        
          if (strpos($output, $replacementText ) === false)
            $modx->documentOutput = str_replace_once(' machinery safety', $replacementText, $output);    
        }
        
        ?>​
        


        For the above to work in the document / chunk you want this to work in add the following:
        <!-- Begin Content -->

          • 3071
          • 99 Posts
          Yeah but it working fine for me! The only thing I want to do now is create some kind of back end or something so I can input the keyword values and href links into it on each site without having to go into the php. No idea how to do this at the moment of course. Another thing would be to place a condition that stops the php placing anymore than 3 links onto one page (for 3 separate keywords that is)

          This stuff is all available in the Wordpress SEO plugin Smart Links. Just need to emulate it somehow!
            • 6511
            • 60 Posts
            danny_kay1710 Reply #45, 16 years ago
            Basically what I changed was to check to see if the replaced version of the keyword was already in the document. If it was then don’t replace it again otherwise do the replace.

            I then in my last post added some extra code to ensure that the bit of content we were parsing was the bit we wanted (so we wouldn’t interfere with menu’s or get multiple instances converted to a link. It did this by looking for a html comment before doing anything. It looked for the comment <!-- Begin Content -->

            I hope this helps you get this all up and running - there may be a more elegant way to do this but I am not 100% familiar with the modX API at this point

            ----------------
            In response to your post whilst I was typing...

            To replicate what the problem was before I made the change - add a New Chunk and put some content in with this keyword.

            Then add it to your document after the first instance of the word has already appeared. You will notice that the first instance of the keyword inside and outside of the chunk will be highlighted.
              • 3071
              • 99 Posts
              Oh I see, so you’d have to use that line within your html before and after the content div areas you want to target, I understand. It picked up a keyword on my H1 tag and placed a link on that actually, I think this is because it was not capitalised though. It also seems to not try to do this to my menus, however, this particular site’s menu is on the right hand div so appears lower down in the code, I need to try another site then maybe will have to use your second code with this in mind. Thanks very much for your help, I am finally getting there!
                • 6511
                • 60 Posts
                danny_kay1710 Reply #47, 16 years ago
                Your very welcome.

                In terms of your backend, if you were to put the keywords in a Template Variable in a comma separated list you would be able to set your keywords per page rather than site wide.

                I will look into it
                  • 3071
                  • 99 Posts
                  I have no idea about that, I am a complete php novice! All I know is that these things can be achieved but I don’t know how! Am going to try this on another site to check it works on older versions, I am assuming it would work on any site wouldn’t it?
                    • 3071
                    • 99 Posts
                    Yeah it is doing the same for me, picking up a keyword in the left nav! I tried your other code and added: <!-- Begin Content --> before my content but it doesn’t seem to recgonise that and still picks up the first link!
                      • 6511
                      • 60 Posts
                      danny_kay1710 Reply #50, 16 years ago
                      Does it modify your menu and the first link or just the menu. Only put the comment in where you would enter the content into the manager - don’t put it in with the menu.

                      Is your menu in the same place as the content in the manager? if it is move the menu into a chuck and leave the <!-- Begin Content --> where it was and it should help.

                      If you need any more help with it let me know.

                      I am just investigating the template variable bit now and trying to get something up and running. I will post it up when I have sorted it out.