We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • How do I actually use a plugin? I create the new plugin, set up the system events I want it to "do its thing" on...now what? How do I actually get it to do anything? huh

    I’ve set up the "ChangeTemplate" plugin; I want it to change the template when I click on a link that basically just reloads the same document, only with the &template=2 added to the URL query string.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 21301
      • 93 Posts
      Quote from: sottwell at Oct 09, 2005, 02:57 PM

      How do I actually use a plugin? I create the new plugin, set up the system events I want it to "do its thing" on...now what? How do I actually get it to do anything? huh
      What about a simple search/replace plugin. You can play around with this and see if it works. Create a plugin on the ’OnWebPagePrerender’ event. Then try the code below.

      The code below actually gets inserted in the parser on the place where the event is called. You extend the parser with your code. You might want to open the source of the parser (manager/includes/document.parser.class.inc.php) and look up where the event is called to get a little more insight.
      $string = $modx->documentOutput;
      
      $pattern = "/cms/i";
      $replacement = "modx";
      $string = preg_replace($pattern, $replacement, $string);
      
      $modx->documentOutput = $string;
      

      • This gets used on every document? What if you only wanted it to work with certain documents?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • Ah, I see it working now! I set up a new "experimental" local MODx installation, and forgot to turn off the page caching! Sorry rolleyes
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
          • Heh! Serious template switcher on the way! This is fun! I do believe that I can use this (along with some serious regular expression work) to carry the language ID across in the URL instead of relying on cookies!
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 21301
              • 93 Posts
              Quote from: sottwell at Oct 09, 2005, 03:58 PM

              This gets used on every document? What if you only wanted it to work with certain documents?
              Glad you got it working laugh It does apply to every document, you’d need to write a document selector (eg based on the template used) yourself. But I’m sure that’s not a problem now that you’ve got caching switched off. wink
              • Got it! Pages that you don’t want to switch put a comment in their template: <!-- donotswitch -->

                Other pages that don’t use that template can still switch back and forth, even to that template and back, but a page with that template assigned to it originally won’t switch. Just took adding another IF condition to the template switcher plugin!

                if(!strstr($modx->documentContent, "donotswitch")) {


                Now I need to set it up to maintain the user’s choice across pages, except for those with templates that can’t be changed.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org