Attachments to pages slow down the site - how do you deal with it? (2 posts)

  1. alanft
    Posted 6 years ago #

    The site I help run has a few pages which we update with new images from time to time, and so the number of attachments to these pages has built up quite a lot over time. The result is a LARGE number of rewrite rules for each attachment which was slowing down the site.

    Now like a lot of sites, we have no interest in the raw attachment pages anyway, so I took the unusual measure of removing all those rewrite rules -- by hacking page_uri_index function in wp-includes/rewrite.php. I could see no way of getting the same results using hooks/filters (though, eg a filter on $attachments in that function would do the trick and I might turn my hack into that as it would make the core hack a single line.)

    I have seen people suggest detaching attachments by resorting to raw if simple SQL changes.

    My question is has anyone got better, cleaner, plugin-based suggestions for dealing with this issue?

  2. alanft
    Posted 6 years ago #

    to answer my own question, i have now restored the hacked rewrite.php and put this in my functions.php instead:

    add_filter('page_rewrite_rules', 'my_page_rewrite_rules');
    function my_page_rewrite_rules($page_rewrite)
    {	return array_filter($page_rewrite, 'my_page_rewrite_rules_callback'); }
    function my_page_rewrite_rules_callback($var)
    {	return (strpos($var,"?attachment=")==false);}

    which has reduced my rewrite rules from 780 to 196, i.e. a quarter of the size!

    this removes every attachment-based URL (where the re-written form includes the string "?attachment=") from pages (via page_rewrite_rules filter).

Topic Closed

This topic has been closed to new replies.

About this Topic