A-  A  A+ RSS Feed

Deep Thoughts by Robert Felty

thoughts on wordpress, latex, cooking et alia

Archive for the 'sql' Category

Wednesday, August 4th, 2010

New WordPress plugin – Image Browser

Screenshot of image browser plugin in action

Screenshot of image browser plugin in action

Today I released my 8th wordpress plugin. This one is quite a bit different from all the other plugins I have written. A friend of mine was looking for a way to create a gallery on his family blog. “No problem!”, I told him, “there are lots of plugins for that”. But I then quickly realized that no plugins were available for what he was looking for. All of the other image gallery plugins for wordpress function by creating galleries manually, say a gallery of wedding photos, or a gallery of a trip to the zoo. My friend was looking for an easy way to browse all pictures he has ever posted on his blog. That is what the Image Browser plugin does. It allows you to browse through all of your pictures on your blog, and also allows you to restrict by year, month, category, and by caption text. It is easy to install and use. Simply install it, then create a new page, and insert the [imagebrowser] shortcode. That’s it. Options can be set either in the settings page, or by using shortcode parameters. If you would like a demo, please take a spin at my family blog gallery, or the image browser page on this site.

Friday, April 9th, 2010

Showing total number of replies in bbpress

showing total number of replies on a bbpress profile page

showing total number of replies on a bbpress profile page

For awhile now I have been wanting to show the total number of topics started and replies in a bbpress forum on the profile page. Today I finally figured out how. I like bbpress quite a bit, as it integrates very nicely into wordpress. The main downside of bbpress right now is that the documentation is still basically nonexistent. Maybe someday I will help out with it.

Anyways, to get the total number of replies, simply use the following little mysql query:

<?php
global $bbdb;
$totalReplies = $bbdb->get_var("SELECT COUNT(post_id) FROM " .
$bbdb->prefix . "posts WHERE poster_id=$user->ID");
?>

To get the total number of topics started, use:

<?php
global $bbdb;
$totalTopics = $bbdb->get_var("SELECT COUNT(topic_id) FROM " .
$bbdb->prefix . "topics WHERE topic_poster=$user->ID");
?>
Saturday, January 2nd, 2010

WordPress 2.9 image changes

WordPress 2.9 has several new image enhancements. One of the biggest features is some basic image editing functionality. Another one is that you can now specify different alt text from the “caption” field. The “caption” field places a caption under the image. The “alt” text is used to describe the picture to non-seeing users (including search engines). This is a nice addition. However, I usually like my caption, alt text, and title text to be all the same, and I don’t like to have to enter it all manually or copy and paste. By default, wordpress will use an IPTC caption as its “description” field, which shows up in the title attribute of the image. This is nice, since I can add a caption in my image editing program of choice (Picasa) and then I don’t have to enter it again. Except for those pesky alt and caption fields, which are blank by default.

This is particularly important if I am uploading many pictures. So I wrote a little sql which will set the “caption” and “alt text” fields to be the same as the description. I already had this working for the “caption” field for quite some time, but getting it to work for the different alt text handling in 2.9 was a bit tricky, since I discovered that the alt text is not stored in wp_posts like the other fields. Instead it is stored in the wp_postmeta table, which is new in 2.9. Although it took me awhile to figure this out, the new table is a welcome addition. Now for each image you upload, wordpress stores meta information in this table, including the width and height, different sized versions of the file, the IPTC caption, some EXIF info and a few other goodies. This means that if you like to include EXIF info about your pictures, that doing so requires only a simple database lookup, instead of having to read the headers from the image, which should be considerably faster I would imagine.

Now for my little SQL which sets the “caption” and “alt text” to be the same as the description. I run this on my server after uploading pictures. It would be even better if I could figure out how to do this as a wordpress plugin.

-- First we set the image caption (post_excerpt) to be the same as the
-- description (post_content)
UPDATE wp_posts SET post_excerpt=post_content WHERE post_type='attachment' AND
date(post_date)=curdate();

-- Next we set the alternative text to be the same as the post_excerpt
INSERT INTO wp_postmeta (wp_postmeta.post_id, wp_postmeta.meta_value) SELECT
DISTINCT wp_posts.ID, wp_posts.post_excerpt FROM wp_posts, wp_postmeta WHERE
wp_posts.ID=wp_postmeta.post_id AND post_type='attachment' AND
date(post_date)=curdate();

-- This line sets the correct meta_key for the previous line, which doesn't
-- seem possible otherwise
UPDATE wp_postmeta SET meta_key='_wp_attachment_image_alt' WHERE meta_key IS
NULL;
Thursday, December 20th, 2007

Releasing the Collapsing Archives WordPress Plugin

Finally getting around to releasing some more plugins. I started using the Fancy Archives plugin by Andrew Rader about the same time I started using his Fancy Categories plugin (maybe in the reverse order actually). I have been modifying it for some time now, and it seems appropriate to release it as a new plugin. The functionality is best described by simply looking at my archives list on this blog. The default wordpress archives list is a simple unordered list. This plugin gives it some dynamic capabilities, similar to the default on Blogger. I have decided to keep increasing the version number from what Andrew was using, so I am calling this version 0.6. Here are the main highlights:

  • Changed name from Fancy Archives to Collapsing Archives
  • Changed author from Andrew Rader to Robert Felty
  • Added option to link to archives.php
  • Added option to list in chronological or reverse chronological order
  • Added triangles which mark the collapsing and expanding features
    That is, clicking on the triangle collapses or expands, while clicking
    on a month or year links to the archives for the said month or year
  • Changed behavior from starting all expanded and then collapsing on page
    load to the opposite
  • Removed the rel=’hide’ and rel=’show’ tags, because they are not xhtml
    1.0 compliant. Now uses the CSS classes instead

You can download it from the WordPress plugin repository.

Wednesday, December 19th, 2007

Releasing the Collapsing Categories WordPress Plugin

I started using the Fancy Categories plugin by Andrew Rader about 6 or 8 months ago. I have been slowly modifying it for some time now, and it seems appropriate to release it as a new plugin. (Note that I thought that Andrew Rader disappeared as well, but I now have found him at his new home at void*. The functionality is best described by simply looking at my categories list on this blog. The default wordpress categories list is a simple unordered list. This plugin gives it some dynamic capabilities, similar to the default on Blogger. I have decided to keep increasing the version number from what Andrew was using, so I am calling this version 0.2. Here are the main highlights:

  • Changed name from Fancy Archives to Collapsing Archives
  • Changed author from Andrew Rader to Robert Felty
  • Added triangles which mark the collapsing and expanding features That is, clicking on the triangle collapses or expands, while clicking on a month or year links to the archives for the said category. This uses html entities (dings) instead of images, for a variety of reasons
  • Lists the titles of posts, instead of just listing subcategories
  • Removed the rel=’hide’ and rel=’show’ tags, because they are not xhtml 1.0 compliant. Now uses the CSS classes instead
  • MOST IMPORTANTLY — it is compatible with both the pre 2.3 database which uses categories, and the 2.3+ database structure which uses the tag taxonomy

You can download it from the WordPress plugin repository.