A quick fix for the impossibility of building new menus on a WordPress site with a large number of pages

I’m working on a theme migration for a WordPress site that as a lot of pages. A lot of pages. 451 pages.

The old version of the site was using page menus, not custom menus, but it was relying on a constellation of abandoned plugins, and that approach just won’t work for the new theme. We need to build custom menus.

The problem is, building a new menu on a WordPress site that has 451 pages is a daunting task. Granted, only 39 of these pages need to go into the main navigation. (A lot of them really should probably be deleted, but that’s the client’s call, not mine.) So, I’m glad I don’t have to wrangle over 400 pages into a menu, but even 39 can be difficult given the shoddy interface WordPress provides for finding pages to add to your menu.

There are three tabs: Most Recent, View All, and Search. Most Recent is useless unless you’re just adding recently created pages to an existing menu. View All is useless because it’s paginated, inside this tiny box, and it lists the pages in an entirely inscrutable order. (OK, it’s not inscrutable. I can scrute it. But just because I understand the logic of how they’re ordered doesn’t mean that order is easy to work with.) And lastly we have Search which seems like the saving grace. But it’s actually the most maddening of the three because of two things:

  1. WordPress search sucks. It doesn’t give extra weight to page titles; it searches the full content. So even if I type in the exact title of the page I want, it’s usually not first in the list. And that’s a problem because…

  2. It only returns 10 results. That’s it. Ten. No lazy loading of more, no pagination, nothing. If your page doesn’t come back in the top 10 results, it may as well not exist.

I’d really love to rebuild the internal WordPress search engine to be smarter about weighting titles. Well, OK, no I wouldn’t. That would be a project I would not enjoy. But I would like for it to be done by someone.

Since that’s not likely to happen, at least there’s a way we can modify the search results to change the number of pages returned. I found the solution, as I often do, on StackExchange. But I dislike a few of the answer’s coding conventions, and I wanted to make one specific change for myself, so here’s my version. (You may not like using closures, especially in a scenario like this because it prevents you, or anyone else, from being able to remove this logic elsewhere. If I were writing a public plugin for this, I’d definitely make it a named function, but this should be fine for a custom theme.)

add_action('pre_get_posts', function($query) {
  if (is_admin()) {
    if (isset($_POST['action']) && $_POST['action'] == 'menu-quick-search' && isset($_POST['menu-settings-column-nonce'])) {
      if (is_a($query->query_vars['walker'], 'Walker_Nav_Menu_Checklist')) {
        $query->query_vars['posts_per_page'] = -1;
      }
    }
  }
  return $query;
}, 10, 2);

The important difference between the StackExchange sample and my code is that I changed the results from 30 to -1 which, in the WordPress universe, equals . Fun!

The standard warning is that setting posts_per_page to -1 is inherently risky because it could cause performance problems. But in my testing of this change, it does not appear to be an issue on this site with 451 pages, so I’m guessing it won’t be for you, either.

Now, instead of getting back a paltry ten results, you’ll get all the results that match your search. And the exact page title you typed in should be in there, somewhere.

The Computer Course #3: History of the PC

For a brief introduction to this blog series, start here.


For the third installment in this series I took a slightly different approach. Rather than preparing a lecture, I had my son do a research project and report. At some point in the near future I will flesh out my own version of the report but for now, here’s the assignment. (I have linked each of the 10 topics to its Wikipedia page as a starting point.)

Research Assignment: History of the Personal Computer

The topic is the history of the Personal Computer. There are 5 pieces of hardware and 5 operating systems I want to focus on.

Hardware

Apple I
Apple II
IBM PC 5150
Commodore 64
Lisa and the original Macintosh

Software

MS-DOS
Original Macintosh System (and how it was inspired by Xerox Alto)
Windows 3.1
Windows 95
Mac OS X (and how it was based on NeXTStep)

Write a brief paragraph (3-5 sentences is good) for each, 10 paragraphs total. Important things to include are the years they were produced, any particular people who played an important role in the development, and what made them each unique and influential.

The report should also have an introduction paragraph where you explain the overall topic, and a conclusion paragraph where you summarize what you found most interesting, or how you think the different systems are related or influenced each other.

Wikipedia is a good place to start, but also check the References section at the bottom of each Wikipedia entry, and look at at least one reference source for each. At the end of the report, include your own References section listing all of the links you used.

Coming soon from Room 34: Rubbish Bin Salvage

Room 34: Rubbish Bin SalvageAs I’m looking forward to some exciting new music projects (including some multimedia and live performance possibilities), I’m also looking backwards — to a growing pile of abandoned musical ideas, some of which are nearly complete, some of which are just the roughest of rough sketches, but all of which I put some time into at one point or another, but never finished… and probably never will.

I’ve decided to bare my soul and put these unvarnished (well, mostly) musical efforts out into the world, to see what comes of it. I’ve identified 22 tracks, 65 minutes of music, or at least music-like sounds, and am in the process of assembling them, warts and all (or sometimes just warts), to be released as a free download here. That’s free as in beer and as in speech. I’ll be releasing these tracks with a Creative Commons license to encourage anyone who might find even the slightest shred of a worthwhile musical idea in these tracks to take them and run with them.

I’m calling the album, such as it is, Rubbish Bin Salvage: Rough Mixes, Outtakes & Other Detritus. I even spent nearly five minutes designing cover art for it using a nifty and appropriate stock photo I found.

All that’s left to do is zip up the MP3s and post the file for your downloading pleasure. But since I am also inclined to write copious liner notes no one will ever read, that part will have to wait until at least tomorrow. So for now, enjoy the cover art.

Technology Large and Small

Little girl in front of a big TVHere I sit in the living room, tentatively tapping out a blog entry on a tiny on-screen keyboard in the new iPhone version of WordPress. (So far, so good.) Meanwhile, the kids are watching a “Lazy Town” DVD on our new, gigantic 42-inch LCD HDTV, purchased last weekend. It’s an interesting study in technological contrasts. These days, everything’s profoundly huge or tiny, depending on the need for mobility. Nothing too deep, I know, but it’s cast in bold relief for me at the moment. Plus, it was a good excuse to test the iPhone app… and to post a photo of the giant television.

Forget what I said before; we have a winner!

It’s a given that anything I post here is going to be brain-deflatingly stupid. But this one goes beyond even what I would have expected of Microsoft. Fortunately, thanks to a highly effective Google search, I was able to solve the problem in minutes.

The problem was this: For a site I’m working on, we are manipulating the 404 error feature to allow users to set up customized URLs. If the URL entered doesn’t match a real page, it gets fed into this 404 error script, which looks up the path in a database and redirects the user to their customized page. A bit of a hack, but it’s pretty slick.

As usual, I am developing the site using Firefox as my test browser. But… UH-OH! Surprise! When giving a demo of this feature today using Internet Explorer, we discovered it didn’t work! Internet Explorer was not returning the server’s 404 error page, instead using its own internal version (which we’ve all seen and most generally ignore).

Drat! What to do? Well, as it turns out (thanks to the aforementioned Google search), the problem is quite simple. To quote the unbelievable but, as I verified, 100% accurate description I found on this page:

Internet Explorer has a lightly-documented “feature” that stops it from serving any custom 404 error page that is less than 512 bytes long. Your visitors will instead be sent to IE’s own 404 page, which is generic and suggests they use an MSN search to “look for information on the Internet.” That’s one way to lose visitors! Make sure your custom 404 error page is over this limit — about 10 full lines of text and HTML should be enough.

Yep, that’s it. I did my best Bart Simpson-at-the-blackboard impersonation, filling my 404.html file with a large comment block wherein I repeated (about 130 times, for good measure) the phrase “This block exists solely to force Internet Explorer to load this page.”

Sure enough, it worked. D’oh!

Thinking a bit more about this, I at least think I understand why Microsoft chose to do this. “If the server’s default 404 error page is so short,” I imagine them musing, “then it probably doesn’t offer users much helpful information. And since our wonderful web browser is much more important to our users than the web pages themselves, let’s just butt in and do things our way. (Is there really any other way anyway?)”