Category: Development

  • Finding the length of WordPress Posts

    I’m just finished converting my plugin pages to use my new WP README Parser plugin. However, I had a nagging feeling that I might have missed, at the very least, 1. But how can I easily tell without going through them all one-by-one?

    Well, their length is a give-away as instead of pages of text, there’s now a single call to my plugin. A quick bit of SQL will show the length of each post and/or page in characters, allowing me to see if any of the original versions still exist.

    Here is an example using standard table naming and will list the details of pages…

    SELECT ID, post_title, guid, length(post_content) AS 'Post Length'
      FROM `wp_posts`
     WHERE post_type = 'page'
       AND post_status <> 'draft'
       AND post_status <> 'trash'
     ORDER BY length(post_content)

    That 3rd line can be modified to WHERE post_type = 'post' for posts, or combined with the original to list both posts and pages.

    Otherwise, it simply lists the ID, title and URL for each page as well as the character length of the page content. It excludes drafts and anything in the trash. The final list is sequenced by the length of the page.

    Not sure if this is useful to anyone, but just in case….!

  • WordPress README files and Markdown

    For those who’ve not come across Markdown, it’s a way of using simple keyboard characters in a text file to indicate formatting. For example **this would be bold**.

    WordPress uses Markdown for the README files that accompanies each plugin – this same file is used to display the plugin details on the WordPress site. Therefore, as a plugin author, I am required to produce Markdown format files.

    At the moment, because I want to have my plugin details on my site as well, I copy and paste the README file to a WordPress post and re-format it. Now, you can change WordPress to use Markdown, but it requires switching off the standard editor as otherwise it will modify the results.

    One of the authors of Markdown, John Gruber, provides conversion scripts in Perl, as well as instructions for modifying WordPress. What I would ideally like, though, is a way to display README files on my site, as required. There is a plugin that will already do this but it does the conversion itself and, well, not very well. The results are iffy to say the least.

    To make matters worse, WordPress’ implementation of Markdown does not match the standard design.

    As with many of my plugins, the need to be able to do something myself has lead me to write a solution. Michel Fortin has converted John Grubers script to work under PHP and, using this as a basis, I am creating a plugin that will display WordPress format README files in a post.

    I’m still working on it, but the principle is that I read in the file and, first, convert the WordPress format to standard Markdown format. From there I can use Michel’s script to do the final conversion. So far, it’s looking good and I’m spending time now adding extra features, such as the ability to miss out README sections (for instance, the installation instuctions) – otherwise, the README is displayed as it is in the actual file. It finds WordPress username and tags and adds automatic links and I’m going to add compatibility with my Simple Content Reveal plugin, so that each section can be collapsed and revealed.

    I’ve already had to speak to Michel about it after having some initial issues, and he’s been very good and helped me out.

    Look out for it in the coming weeks!

  • WordPress & Spam Pingbacks

    My posts often receive pingbacks – these are other blogs that have referenced my post. However, I still insist on manually authorising these, like comments, because they too can be prone to spam.

    These spam sites are often WordPress sites which are set up with plugins installed that are designed to trawl and integrate content from other sites. They then plaster adverts all over their site and then leave the site to make revenue. They become popular because of this back-linking to the other sites.

    And, visiting them it is often obvious that they are a spammer due to a total lack of its own content and, my personal favourite, visting the “About” page and finding this…

    This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.

    Yep, that’s the default content of this page after a fresh installation of WordPress – they can’t even be bothered to fill this in.

    So, here’s an example. My review of the Logitech C510 webcam got a Pingback from a website named “Review Central”. Yep, no original content, no effort in the site design, adverts everywhere, a default “Meta” list in the sidebar, and the aforementioned “About” page. It’s spam, and marked as such.

    Update: Ahh, the irony. The same day I published this post, I received a pingback on it from a spam site picking up on me mentioning the Logitech webcam. So they’re now proudly displaying my post on their spammy site which complains about shoddy spam sites who just publish other people’s posts. Nice.

  • Improving WordPress Search Further

    I wrote an article some time ago highlighting ways to implement search improvements to WordPress (and the default search DOES suck). Now, I’d like to update you on where I am, including some further changes that I made last night.

    Current Set-up

    My current set-up is that I’m displaying 3 posts per page. However, in the case of search results, I’m displaying 12 per page, as I display only an excerpt and restricted social sharing and other output. The idea, like the results from a search engine, is to find the post or page that you’re after and click on it for the full version.

    Recommended Plugins

    By default,WordPress only allows one “posts per page” value. To achieve this, you’ll need to read my original post which shows you how to override the search results to show more. I still use the Results Count plugin as well, which displays how many search results were returned.

    In addition, on all post and page types I use the WP Page Numbers plugin to allow users to easily scroll across multiple pages, rather than use the default WordPress “Next” and “Prev” buttons.

    Finally, Search Meter is an excellent plugin for finding out what your visitors are searching for.

    Modifying the Search Results

    Last night I had a brain-wave. After performing a search myself, I had one result. Except it was the usual excerpt and I had to click on it to view the full version. But, if there’s only one, why do this? Indeed, if there’s 3 or less, why not display the full posts, and only collapse to the excerpt version if there’s more than this?

    And that’s what I’ve changed – try it! Look for “wordpress“, for instance, and you’ll find pages of potential results, all displayed as limited excerpts. Look for “avforum” and one result will be returned and the entire post will be shown. Lovely.

    To do this I simply compare the results of $wp_query->found_posts with get_option('posts_per_page'). I do this outside of “the loop”.  The former will return the total number of posts being returned and the latter will return the number of posts per page that you’ve your WordPress to. In my case, this latter value is 3.

    So, if a search is performed and 1 results is returned out of 3, my theme can make the decision to output the full version of the post instead of the smaller version. I can also make the decision at this point whether to override the number per page to 12 or not (if I’m display the excertp versions).

    I hope this makes sense – please comment if you need any further clarity on this.

  • The WordPress Loop

    I often get asked about “the loop” in WordPress, as many of my plugins require knowledge of it for successful integration.

    “The loop” is simply some WordPress code that handles the fetching of post or pages. Each WordPress theme has a number of files that handle different types of output – for instance, index.php is used for displaying a number of posts, page.php displays pages and single.php shows a single post. Each of these contains the code for the loop.

    The first line is usually this…

    <?php while (have_posts()) : the_post(); ?>

    And the last line is usually…

    <?php endwhile; ?>

    Everything in between is the setting up and displaying of the post/page in question.

    So, let’s use Simple Social Bookmarks as an example. You will probably want to put the function call for this just before that last line of the loop – that will it will be the last thing displayed after each post or page. However, you may want other content to appear first, in which case you will choose a different location between the above 2 lines to put it.

    What I can’t tell you is exactly where you need to put the code – that’s entirely down to you and how you wish the output. Indeed, there’s no reason why, in the case of Simple Social Bookmarks, you have to put it in the loop – you could have one at the top of each page. Bear in mind, however, that if you put code outside of the loop then it may not be able to get certain information – in out example, you’d have to supply a URL, as normally this is supplied by the loop to indicate the current post/page.

    I hope this makes sense. If not, you can read more about it at WordPress.org.