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

Gaarai gaarai at gaarai.com
Mon Jan 19 03:07:08 GMT 2009


Hi Lynne.

Why not create a filter and use it in your header.php file?

For example:

    <?php $page_doctype = '*<!DOCTYPE html PUBLIC "-//W3C...>*'; ?>
    <?php $page_doctype = apply_filters( 'alter_page_doctype',
    $page_doctype ); ?>
    <?php echo $page_doctype; ?>
    <html ...>
    ...

The bold portion needs to be modified to be whatever your default 
doctype is.

Then all you have to do is latch into this filter via your plugin and 
put the necessary code in the filtering function that will determine 
when the conditions are right to change the doctype. Actually, you can 
do the conditional before you add the filter so that the function 
doesn't even run if it doesn't need to.

For example:

    <?php
        if ( preg_match( '|^*/special/*|', $_SERVER['REQUEST_URI'] ) )
           add_filter( 'alter_page_doctype', use_custom_doctype );
       
        function use_custom_doctype( $doctype ) {
           return '*<!DOCTYPE...>*';
        }
    ?>

This code basically looks for any URL of the format 
http://domain.com/special/ or http://domain.com/special/something-else/. 
In other words, it matches anything that has /special/ directly after 
the domain. It then adds the filter to output a custom doctype.

An alternate example that only matches a specific request is as follows:

    <?php
        if ( '*/special/*' === $_SERVER['REQUEST_URI'] )
           add_filter( 'alter_page_doctype', use_custom_doctype );
       
        function use_custom_doctype( $doctype ) {
           return '*<!DOCTYPE...>*';
        }
    ?>

This only works for a request of the form http://domain.com/special/ and 
nothing else.

In each of the last two samples of code, there are two portions in bold.

The first bold portion is what you should change to match the page/post 
link that you are targeting. Note the beginning and ending forward 
slash. The format is extremely important. if you mess it up, the match 
won't work.

The second bold portion is where you put your custom doctype.

Doing the customization this way, you can modify your doctype anyway you 
want and won't be boxed in a corner later on in case you need to modify 
how this doctype change functions. Also, it fits in exactly with the 
coding style of WordPress.

I tested this out by adding the first piece of code to my test site in 
the header.php file in place of the standard doctype definition. I then 
loaded the second bit of code in my functions.php file. I tested it out 
on the page I put in the code, and it worked like a charm.

Good luck.

Chris Jean
http://gaarai.com/
http://wp-roadmap.com/



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
>
>   


More information about the wp-hackers mailing list