Categories
WordPress

Improving WordPress Search

WordPress search sucks. It’s well known.

A friend has been looking for what he thinks is the solution and, let’s be honest, he’s got it pretty much right.

He believes any half-decent search should…

  1. return posts by most relevant first
  2. do proper searches based on the phrase you submit
  3. indicate which words matched the post
  4. show how many posts matched
  5. list the posts by just title, or summary or full and allow you to switch
  6. show which page you’re on, if there are more than one page of results

I’m not sure about point 5 – your average search engine, for instance, will give a summary and title and allow you to click it for the full details. I think a search should work like that.

Unfortunately my friend hasn’t found a solution..

I’ve looked at various plugins, but not really had any luck finding one which fixes all the problems.

So, not knowing whether I would be able to solve this dilemma with existing plugins or if I’d end up writing my own, I set about finding an answer.

And I did.

Here’s what you need to do.

Improving the Search Results

Install Search Unleashed. This will add an improved search ability, will show post summaries, highlight searched text and display results in order of relevance.

I also found I had to manually update my themes search.php file to change the rest of the summarised output – removing the calendar image, etc. You don’t have to do this, but it provided a nice compact result.

Show the Number of Results

Install Results Count. You will then need to add some code not only to your search.php theme file but (if you want) to archive.php. This will now display the number of results and which ones you are current viewing. For example, Showing results  1 - 16 of 32 for the search term:  wordpress.

Install WP Page Numbers. Again, you will need to follow the instructions for inserting the appropriate code into search.php. The configuration options will also allow you to change the style of the output. What this plugin does, in a nutshell, is display the current page of results plus lists the other pages available, and let’s you move between them. For example…

Line Numbers Example

Increasing the number of Search Results Per Page

The one problem with summarising your search results is that the blogs are a lot smaller on the page. WordPress only gives you one place to specify the number of posts per page and this applies to all forms of output – this means that the summarised results will take up a lot less of the page and, if there’s a lot of results, you may be aching to increase this number.

In the case of my blog, I have the number of posts set to 5. I found that the number of summarised search results would look best at 16.

Unfortunately, this is a bit of a manual fix. First of all, dig out your themes search.php file again. Hopefully you should be able to find the following line of code within it…

<?php if (have_posts()) : ?>

When you have, insert the following just before it…

<?php
$wp_query->query_vars["posts_per_page"] = 16;
$wp_query->get_posts();
?>

This will override the number of posts just for the search results. This solution doesn’t work for the Results Count plugin though, so you will need to change this too. As it’s a change to the plugin code, be aware that you may need to make this change again if you ever update the plugin.

In results-count.php, find the line…

$numposts = $wp_query->found_posts;

Now, insert after it…

$posts_per_page=16;

The Result

I think that covers pretty much all of what Tony was after. Except point 5.

If you’d like to try the results out then, well, do a search on this site. Feedback is always appreciated!

Categories
WordPress

Adding your own WordPress Plugin

Having just created and submitted my first WordPress plugin, I thought I’d go through the process. And I have to say, WordPress don’t make it easy.

Assuming you have your plugin written and tested, you first need to submit it to WordPress for them to create a subversion repository (basically an archive where you can store it).

  1. Make sure you’re signed up (and signed in) to WordPress.org and head over to the Developer section within the Plugin Directory.
  2. From here, there’s an option named “Add Your Plugin“.
  3. You need to enter the name and description of your plugin and, optionally, a link to it.

That’s it for that. Now you need to wait for WordPress to send you a link to your repository.

Meantime, you need to create a readme.txt file to go with your plugin. This will not only be bundled with your download but will be used for your plugin entry on the WordPress site. What I didn’t know is that these files, although plain text, have a special markup language within them.

  1. WordPress provide an example, and I just took this and modified it appropriately.
  2. There is also complete documentation on the markup language used.
  3. Once you’ve written it, you can validate it.

So, now you have your plugin, a valid readme.txt and, hopefully, a repository URL from WordPress.Now it all got a lot fuzzier.

The WordPress instructions lept straight into a load of unexplained commands, not explaining, amonst other things, that you need SVN installed to use these. I came across another blog which recommended installing TortoiseSVN, which has a GUI interface. I did this but soon found the instructions on this alternative blog didn’t match what I was seeing (possibly a different version of TortoiseSVN?).

What I did was to use the Repo-browser context menu option. This provides you with a drag-and-drop interface to your repository (you use the URL that WordPress supplied plus your WordPress.org username and password). Head back to the WordPress site instructions on ensuring that you place your files in both the “trunk” and create another, versioned, copy under “tags”.

Once this was done my WordPress plugin page sprang to life, taking its details from my readme.txt file.

And that was it.

Categories
WordPress

Displaying code in WordPress

After experiencing some problems around displaying code in WordPress (mainly around the code lines being too long and wrapping off the side of the page) I installed Dean’s Code Highlighter.

Now, don’t get me wrong, this is an excellent plugin. However, I found a flaw in it after someone reported a problem with the WordPress plugin that I launched yesterday. They’d copy and pasted the example code from my instructions page and it didn’t work. It turns out that the code highlighter had taken my single quotes and converted them to open and closed single quotes. Which broke the code.

I had a good look at the plugin code but couldn’t easily work out a solution. In the end I realised that I simply didn’t need code highlighting so turned the whole thing off. Instead, I used a similar style and applied to the bog standard CODE wrap. The only down-side is that I have to wary of the line wrapping again.

Categories
WordPress

WordPress Tags

I’ve found – and I know others have too – that you can’t delete tags in WordPress from the tags management screen.

However, after recently upgrading my WordPress version I found that, after leaving my plugins disabled by accident, it would work. Enabling my plugins then broke the functionality. Unfortunately, my good fortune didn’t go as far as thinking to test it and try and work out which plugin is the cause.

So, yes, that’s the cause. But exactly what I’m not sure.

Next time I have some tags to delete and some times on my hand, I’ll try and work it out further.