Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

hey, i have the following page template for a sitemap.

<?php
/**
 * Template Name: Sitemap
 */
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div>
        <h3>Pages</h3>
            <ul>
                <?php wp_list_pages('title_li=&depth=0&exclude='); ?>
            </ul>
        <h3>Posts</h3>
            <?php $first = 0;?>
            <ul>
            <?php
            $myposts = get_posts('numberposts=-1&offset=$first');
            foreach($myposts as $post) :
            ?>
            <li><a href="<?php the_permalink(); ?>#b"><?php the_title(); ?></a></li>
            <?php endforeach; ?>
            </ul>
        <h3>Categories</h3>
            <ul>
                <?php wp_list_categories('title_li=&orderby=name'); ?>
            </ul>
        <h3>Tags</h3>
            <ul>    
                <?php
                $tags = get_tags();
                foreach ($tags as $tag){
                    $tag_link = get_tag_link($tag->term_id);
                    $html .= "<li><a href='{$tag_link}#b' title='{$tag->name} Tag' class='{$tag->slug}'>";
                    $html .= "{$tag->name}</a></li>";
                }
                echo $html;
                ?>
            </ul>
    </div>

<?php endwhile; endif; ?>

It's my intention to not define a header with or or anything like that, because this thing is only used to get loaded via an ajax call.

However the output of this thing looks like this

<div> 
    <h3>Pages</h3> 
    <ul> 
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
        <html><body> 
            <li class="page_item page-item-87"><a href="http://domain.com/account#b" title="Account">Account</a></li> 
            <li class="page_item page-item-67"><a href="http://domain.com/forum-3#b" title="Forum">Forum</a></li> 
            <li class="page_item page-item-107"><a href="http://domain.com/map#b" title="Google Map">Google Map</a></li> 
            <li class="page_item page-item-79"><a href="http://domain.com/contact#b" title="ContaCt">Contact</a></li> 
            <li class="page_item page-item-92"><a href="http://domain.com/login#b" title="Login">Login</a></li> 
            <li class="page_item page-item-93"><a href="http://domain.com/register#b" title="Register">Register</a></li> 
        </body></html> 
    </ul> 
    <h3>Posts</h3> 
    <ul> 
        <li><a href="http://domain.com/test-post#b">Test post</a></li> 
        <li><a href="http://domain.com/lorem-ipsum-test-page#b">Lorem Ipsum Test Page</a></li> 
        <li><a href="http://domain.com/hello-world#b">Hello</a></li> 
    </ul> 
    <h3>Categories</h3> 
    <ul> 
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
        <html><body><li class="cat-item cat-item-1"> 
            <a href="http://domain.com/category/something#b" title="Show all posts">Something</a> 
        </li></body></html> 
    </ul> 
</div> 

Any idea why there is a weird doctype declaration and a <html><body> inserted inside of every ul. Except for the Posts ul. Just weird. Why could that happen and what causes this? I actually use a html5 doctype on my website.

Thank you for the help

edit:

add_filter('wp_list_pages', 'add_hash'); /*Add #hash to wp_list_pages() function*/
add_filter('wp_list_categories', 'add_hash'); /*Add #hash to wp_list_categories() function*/
function add_hash($output) {

        $dom = new DOMDocument();
        $dom->loadHTML($output);

        $a_tags = $dom->getElementsByTagName('a');

        foreach($a_tags as $a)
        {
            $value = $a->getAttribute('href');
            $a->setAttribute('href', $value . '#b');
        }

        return $dom->saveHTML();
}

The new DOMDocument is adding this doctype declaration and the and

share|improve this question
1  
Is this the real source code or just the DOM created by the browser? –  toscho Mar 26 '11 at 12:24
    
this is the real source when I look it up in the browser (browser view source - not just a dom inspector) ... all browsers print the same source code. –  mathiregister Mar 26 '11 at 12:36
    
what do you mean by real source though? How can i look up a real source code...the real source code is the sitemap.php file...and the source that's generated is the one i posted above. –  mathiregister Mar 26 '11 at 12:45
    
Yes, I meant the source view in the browser. Your output looks like it comes from a different template: the trailing #b is missing and the entire tag list too. –  toscho Mar 26 '11 at 13:04
    
oh sorry, i got rid of the trailing #b so it doesn't look confusing - ( bad idea :) )... no it's the same template - i just wanted to simplify the output for this question here so it doesn't look confusing. –  mathiregister Mar 26 '11 at 15:31

3 Answers 3

up vote 0 down vote accepted

Search all your theme and plugin files for:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

Disable all plugins and re-enable them step by step. This document type declaration is not part of WordPress’ core files; there is a filter active somewhere.

Update

Try to remove all filters from the affected functions. Start with wp_list_pages():

remove_all_filters( 'wp_list_pages_excludes', 99 );
remove_all_filters( 'wp_list_pages', 99 );

I still don’t believe that the doctype is nowhere to be found. That is impossible.

share|improve this answer
    
good point. However not even a single file contains this declaration. Any other ideas - this behaviour is just strange. can I try to remove filters on this page? –  mathiregister Mar 26 '11 at 16:06
    
@mathiregister I have added some code to disable the filters. It would really help if could just point us to the URI of the document. –  toscho Mar 26 '11 at 16:21
    
i got it! remove_all_filters works fine, thank you. However the problem is not a plugin is adding this doctype declaration but I am doing it. I'm using the DOMDocument class to add a #b hash to each page-link and category-link. I updated my question. maybe you have an idea how I could prevent my filter from doing that? –  mathiregister Mar 26 '11 at 17:54
1  
There is a filter for 'post_link' in get_permalink(). You could add your own filter at the start of your template and insert the string without any DOM manipulation or regex. –  toscho Mar 27 '11 at 0:21

The DOMDocument approach seems like an overly complicated one for what you're trying to do. Couldn't you just do this with a regex filter?

add_filter('wp_list_pages', 'add_hash'); 
add_filter('wp_list_categories', 'add_hash'); 

function add_hash($output) {
    return preg_replace( 
        '/<a\s(.*?)href="(.*?)"(.*?)>/', 
        '<a $1href="$2#b"$3>', $output );
}

(written quickly, apologies if this regex doesn't catch everything... its just for the concept though...)

share|improve this answer

How are you loading this template? Is it being included within another template file that already calls get_header() and get_footer(), or is your AJAX call creating a new window (i.e. a new document)?

Also: why are you putting your content inside of a Loop? For one, you never output the_content() or the_excerpt(); for another thing, I assume that, for a Sitemap, you don't intend to add any Page content, but rather merely assign the template to a given Page?

share|improve this answer
    
it's going to be included inside of a nother page the already has get_header() and get_footer() included (like you said)... i didn't know i could get rid of the loop - now i got rid of it. however that doesn't change anything to the fact that there is this doctype inserted. –  mathiregister Mar 26 '11 at 15:33

Your Answer

 
discard

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.