A-  A  A+ RSS Feed

Deep Thoughts by Robert Felty

thoughts on wordpress, latex, cooking et alia

Archive for the 'sql' Category

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.

Monday, May 14th, 2007

Chronologically ordered blog in wordpress

I recently started the FedDibblety house blog, documenting our home improvements to our new home in Indiana (which Clare’s parents built). I set up accounts for my wife Clare, her parents, Dave and Ellen, and my parents, Harold and Fran. Ellen, being a technology nut like myself, immediately jumped in writing posts about the house, and also had some feature requests. One of those features was to have the blog read in chronological order. Most blogs read in reverse chronological order, but since this one was kind of diary-esque, it seemed more appropriate to have it in normal order. I am also dating posts corresponding to the improvements made, not the actual post date. I started searching around for forum posts and such about how to do this, and I came across two relevant ones. I came across one post on wordpress.org which had a very simple solution, of simply adding one line to the index.php file in the root directory, like so:

<?php
/* Short and sweet */
define('WP_USE_THEMES', true);
$order = 'ASC'; //adding this line
require('./wp-blog-header.php');
?>

This did exactly as one would expect. However, this also caused some problems. For those avid blog readers, now they would have to scroll way down (or go the archives) to see new posts, which doesn’t really make sense. In addition, the RSS feed was also chronologically ordered, so that RSS users would find it very difficult to see if the blog had been updated recently. While searching I also found another post on janegalt.net, in which the blogger asked the same question, and a reply by Jeff Licquia suggested a very simple solution:

To set up archives to use a different template, go into the management page, then go to Options, then Permalinks.

If you can’t, or don’t understand, the RewriteRules stuff, look at the note about not being able to use mod_rewrite. Where it says:

/index.php/archives/%year%/%monthnum%/%day%/%postname%/

Change “index.php” to something else, like “archives.php”, and put that in for the virtual site structure. Copy index.php to archives.php in your document root, then add the special flag to archives.php. You should be all set at that point.

(I’m a newbie to WordPress, but not to Web servers or Web stuff in general, so the above makes sense to me but isn’t tested. I’ll try to help if you have problems.)

This idea worked very well. I made the change in the Wordpress options page, then copied index.php to archives.php, using the $order=ASC flag, and removed that from index.php. Then I had to change the .htaccess file to direct queries to archives.php. I did not want to do this for feeds though, so I had to put in two rules, like so:

#.htaccess file for fedibblety/house blog
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /house/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.*feed\/?$ [NC]
RewriteRule . /house/archives.php
RewriteCond %{REQUEST_FILENAME} .*feed\/?$ [NC]
RewriteRule . /house/index.php
</IfModule>

Then there was one last thing. In that blog (and in this one), I am using the fancy-archives plugin (as I write this post that site seems to be down, so I am not providing a link), which I have tweaked a bit. To get the archives in the sidebar listed in chronological order, I just had to change the SQL queries in the fancy-archives-list.php file to ORDER BY post_date ASC.

That was it!