We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3867
    • 3 Posts
    Hello in an attempt do to do some text replacement on the HTML of a fully rendered document before it is outputted to the browser I encountered a problem.

    I prefer to do this with a plugin, and I know that it is possible to use
    $modx->event->name == 'OnParseDocument'
    &$this->modx->resource->_content;
    

    However this will trigger my plugin code to run for every resource attached to the document. Which I presume can be avoided, I tried various other events like ’OnWebPagePrerender’ but do not know how to reference the fully generated HTML code.

    Any suggestions? Much appreciated!
      • 3749
      • 24,544 Posts
      Use OnWebPagePrerender and grab the page content like this:

      $output = &$modx->resource->_output;


      Then, just modify $output however you’d like and the browser will get the altered content.

      Here’s an example:
      <?php
      $output = &$modx->resource->_output;
      $replacement = '[Expletive Deleted]';
      $words = array('damned','damn','hell'  'crap');
      $output = str_replace($words, $replacement, $output);
      unset($words);


        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
        • 3867
        • 3 Posts
        Thanks Bobray, resource->_output instead of resource->_content did the trick.