We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24495
    • 407 Posts
    Basic: a user as editor puts text into the RTE and defines some subheads as h2.

    In my special output I need all subheads written as h3. I think there is a simple way to replace every [tt]h2>[/tt] with [tt]h3>[/tt] but how? huh
    I’m not the programmer, please give me a helpful kick smiley

    Getting the content: what’s the best way?
    $modx->getPageInfo($id, 0, $fields='content')

    or
    $modx->documentObject['content']


    The replacement will be done with [tt]preg_replace[/tt]? How is the correct notation to replace [tt]h2>[/tt] with [tt]h3>[/tt]?

    And: yes I should and would like to learn PHP wink Thanks for help
      • 21257 MODX Staff
      • 730 Posts
      rasc,
      If you want to replace all h2 with h3 across your whole site, then I’d recommend making a Plugin.
      The Search Highlighting plugin is a good example of a simple plugin that alters the html on it’s way out of MODx.
      This line gets the document output:
      $output = $modx->documentOutput; // get the parsed document
      ...
      

      Then it manipulates $output, and writes it back to the output buffer:
      ...
      $modx->documentOutput = $output;
      


      I would use str_replace if possible instead of preg_replace. str_replace is faster while preg_replace is more flexible.

      So your plugin code might look like this:
      $output = $modx->documentOutput; // get the parsed document
      $output = str_replace('h2>', 'h3>', $output);
      $modx->documentOutput = $output;
      


      Or perhaps it could be optimized like this, which might use less memory:
      $modx->documentOutput = str_replace('h2>', 'h3>', $modx->documentOutput);
      


      After you input the above code into your new plugin (you can name it anything you want, and remember the plugin code should not contain <?php and ?> at the top and bottom), you need to click on the System Events tab and check the "OnWebPagePrerender" event. This will make sure that your code is run at the right time. It will run on every page view across the whole site (except for pages that are cached).
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 24495
        • 407 Posts
        That’s the way but I need a snippet because of I need this replacement only on special pages and not for whole site. But the solution is the same. That’s all I need. Thanks for help!