[wp-hackers] Replace default doctype on one page only through plugin

Gaarai gaarai at gaarai.com
Mon Jan 19 05:14:20 GMT 2009

The idea I sent before works, but it would require modification of the 

The only way I can see of doing this completely inside the plugin with 
not modification of the theme itself is by using the ob_start() function.

For example:

        if ( *$contact_page_displayed* ) {
            add_action( 'get_header', 'start_output_collection', 1000 );
            add_action( 'wp_head', 'send_output', 1000 );

        function start_output_collection() {

        function send_output() {
            $header = ob_get_contents();

            $header = preg_replace( '|<!DOCTYPE[^>]*>|i',
    '*<!DOCTYPE..>*', $header );

            echo $header;

I've tested this on my test site, and it works.

There are two places of interest that I have made bold.

The first one will have to be replaced with logic that determines 
whether or not the contact form is rendered on that page or not. This is 
completely dependent upon exactly which plugin you are using.

The second one is the actual doctype to be output. I know what you are 
going for, but I shortened it to make the code look better in the email.

Now, you just sent a message about wanting a bulletproof way of doing 
it. The way I sent before is bulletproof as it will always work. The 
main issue with the code for mass distribution is the requirement to 
modify the theme.

This solution /seems/ to be ideal then, but it is not. As for 
bulletproof, it definitely is not.

The problem is that the ob_start function does not nest. There is no way 
to guarantee that nothing else will call ob_start between the 
start_output_collection() and send_output() functions. On a standard 
install with the default theme, there are a total of 45 apply_filters 
calls as well as requiring the theme's header.php code. At any point in 
executing any of these sections of code, some code may exist that makes 
use of ob_start.

So, this solution may be useful to you with your own site, but I would 
not recommend it at all for general public use as ob_start should only 
be used in tightly-controlled sections of code where you know for a fact 
that there is no chance that another instance of ob_start will occur.

In closing, you can do what you wish with this code, but I still 
recommend using the first set of code that I sent as it 1) won't cause 
site failure due to unknown code interfering with its operation and 2) 
won't cause other code to fail since it doesn't use the 
possibly-destructive ob_start function.

Chris Jean

Lynne Pope wrote:
> Well, what I am trying to do is this...
> I am using a contact form plugin on my contact page. I have cleaned up XHTML
> validation errors on the form but the form uses Recaptcha for anti-spam
> assistance and the no-script fallback for Recaptcha is an iframe.
> Since DOCTYPE applies to the document and not to an overall site, I decided
> to get pendantic and change my XHTML 1.0 STRICT to XHTML 1.0 TRANSITIONAL
> for that one page alone.
> Now, for my own site I can simply make the contact page into a template and
> call a different header, but having got the bit between my teeth I decided
> it would be more useful if I could find a way to add a DOCTYPE override to
> the plugin.
> I didn't write the plugin (too lazy to write my own contact form) but
> realise that if I have issues with validation for it then others will have
> too. Ergo, if I can find a way to do this safely within the plugin itself
> the code then becomes reusable and worth contributing back.
> I'm not going to hassle the plugin developer over this as I realise not
> everyone cares if they have one page that doesn't validate. Besides, once I
> work out a way to do it without creating more than negligable performance
> issues this will be something I can reuse.
> Lynne
> 2009/1/19 Mike Schinkel <mikeschinkel at newclarity.net>
>> Lynee:
>> Does it even need to be in a plugin?  It this for this site only, or are
>> you trying to create generic functionality?  Can't you do just do something
>> like (in ~psudeo-code):
>> <?php
>> if ($_SERVER['REQUEST_URI']=='special_url') {
>>   echo '<Special DocType>';
>> } else {
>>   echo '<Regular DocType>';
>> }
>> ?>
>> -Mike Schinkel
>> http://mikeschinkel.com/custom-wordpress-plugins/
>> Lynne Pope wrote:
>>> Hi all,
>>> I am working on a site that is using XHTML 1.0 Strict but want to serve
>>> XHTML 1.0 Transitional on one page only. The page uses a plugin so the
>>> change to the doctype could go into the plugin.
>>> I can do a query in header.php but want to avoid unnecessary queries (why
>>> add an additional query to 300+ pages when only one is affected?) but
>> cannot
>>> get an override working for the plugin.
>>> Any ideas?
>>> Lynne
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list