Site icon Random Thoughts

Remove category/author/attachment/etc pages from WordPress

Here’s how: Put this in a WordPress PHP file:

function my_disable_some_pages() {
  if ( is_author() || is_category() ||
       is_tag() || is_tax() ||
       is_attachment() || is_date() ) {
    // Redirect permanently to homepage. 
    wp_redirect(get_option('home'), 301); 
    exit; 
  }
}
add_action('template_redirect', 'my_disable_some_pages');

Remove whatever is_ you want from the if above there — perhaps you want to disable author pages, for instance, but not the category pages? It’s easy to edit.

Why would you want to disable (some of) these pages?

Because WordPress, by default, helpfully adds a whole bunch of index-like pages for authors, categories, taxonomies and the like. These can be useful, but if you just have a simple single author blog without categories, these are a bit annoying. They’ll pop up in search results, often ahead of the real posts (since they may have more relevant search terms), so people will be led to https://my.awesome.blog.example/category/uncategorized/page/27/ where they’ll find nothing relevant — because that’s now on page/28 as you’ve posted more stuff.

In addition, AI scrapers will be slamming your site, so you want to reduce the number of pages to a bare minimum — they’ll still slam your site, but less. The sheer number of different URLs WordPress offers up by default means that if you do one post, there’ll be about a dozen different URLs that give you that post (or excerpts from that post). And these scrapers are not very discerning.

And since no real users will ever access these pages, whatever cache you’re using won’t have them, so it means actually generating the pages.

The WordPress console should have checkboxes to disable these pages, but it doesn’t, so…

Note the mealy mouthed way I started this blog post: “Put this in a file”… The most insane thing about WordPress is that there is no good place to put snippets like this. All recipes say something like “put it in the theme’s functions.php” or “alter your category.php” or some crazy shit like that. The next time you update WordPress/the theme, those changes will be gone.

Why on Earth doesn’t WordPress have a my_functions.php file somewhere where you can put stuff like this?

Presently, unfortunately the only sane way to do stuff like this is to make a plugin (!) and put the stuff there. Fortunately, that’s not difficult:

  1. Find the plugin directory. It will typically be /var/www/wp-content/plugins/.
  2. mkdir my-plugin
  3. Put the following in my-plugin/my-plugin.php:
    <?php
    /*
    Plugin Name: My Plugin
    Description: My Plugin For Stuff
    */
    
  4. Go to the WordPress dashboard and enable the plugin.

Then you can put whatever PHP you want into that file. Fun note: If you have any syntax errors in that file, your entire site will go down.

Fun times!

I felt the need to write this post not because any of this is very difficult, but because he info to do this seems to be oddly obscure these days:

But WordPress knowledge is pretty SEO poisoned, so it’s not all that strange that a straightforward recipe like this isn’t on the Google front page:

Exit mobile version