WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to use WordPress as some meta templating system and include some JSP snipptes which will be evaluated later with Tomcat after rendering the page statically. But naturally the JSP Code could not be interpreted by the php parser. This results in plain text strings mixed with the interpreted template parts.

Is there any way to hook into the (pre) rendering of a WordPress Template?

I need way get access the rendered template so i can strip out the JSP-Tags. Maybe wrapped between some unique tags or something else. But this ist not part of this question.


<?php get_header(); ?>

    <%= request.getParameter("title") %>

<?php get_footer(); ?>
share|improve this question
I don't quite follow your use case. If you don't want these tags in generated page source then why are you including them in template? – Rarst Sep 30 '12 at 22:18
@Rarst Actually it's quite simple. I've a CMS and a Java application running on Tomcat. The CMS -WordPress- must generate static HTML with some JSP fragments which are needed by the Java application to work properly. The Client should be able to view the CMS part without being interrupted by not rendered JSP snippets. – gearsdigital Sep 30 '12 at 22:46
So you are using same output for two distinctly different purposes? Why not just generate different content for users and Java app? – Rarst Sep 30 '12 at 23:18
Yes. Because this is our use case :) It's one application consisting two parts. One Part is generating jsp enhanced static content for the other one which works with the jsp. – gearsdigital Oct 1 '12 at 7:59
up vote 2 down vote accepted

You can, but WP doesn't offer you the possibility to do it in a nice way, so you have to use output buffering.

Something like:

// before template is included
add_action('template_redirect', function(){

  // you can do some checks here
  // perhaps you need this only a certain page?

  // capture output from here on...

    // this function runs after the capture ends,
    // you can replace your tags here 
    $html = strtr($html, array(
      '<render:jsp>'  => '<!--',
      '</render:jsp>' => '-->',

    return $html;



The contents of the buffer are automatically sent by PHP to the output just before the script finishes execution.

share|improve this answer
Thanks! I'll evaluate your suggestion :) – gearsdigital Oct 1 '12 at 8:01
Works as expected. – gearsdigital Oct 1 '12 at 9:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.