sql – robfelty.com https://robfelty.com Thu, 26 Mar 2015 15:38:46 +0000 en-US hourly 1 https://wordpress.org/?v=7.1 89964869 Postgres tip of the day – show size of all databases https://robfelty.com/2015/03/26/postgres-tip-of-the-day-show-size-of-all-databases Thu, 26 Mar 2015 15:38:46 +0000 http://robfelty.com/?p=1307 Here is a handy little query to show the size of all the databases on a particular postgres server:

SELECT pg_database.datname,  
  pg_size_pretty(pg_database_size(pg_database.datname)) AS size  
FROM pg_database;  
  
   datname    |  size  
--------------+---------  
 template1    | 6369 kB  
 template0    | 6361 kB  
 postgres     | 6589 kB  
 foo          | 55 MB  
 bar          | 5129 MB  
 foobar       | 85 GB  
(6 rows)  
]]>
1307
Random featured images in wordpress https://robfelty.com/2011/07/29/random-featured-images-in-wordpress Fri, 29 Jul 2011 22:29:21 +0000 http://robfelty.com/?p=1154 I’ve had random header images on the Fedibblety Family blog for quite some time. I originally implemented this by looking for images in a particular directory. However, I had to manually add pictures to this directory, which was a bit cumbersome. I started thinking, why not just select randomly from all the featured images I have for my posts? So I hacked up a quick function to do that. Here it is:

function get_random_header_images() {
global $wpdb;
$featured_image_query = "select id from wp_postmeta join wp_posts on wp_postmeta.meta_value=wp_posts.id where meta_key='_thumbnail_id' order by rand() limit 10";
$img1 = '';
$img2 = '';
$featured_images = $wpdb->get_results($featured_image_query);
foreach ($featured_images as $img) {
$img_src = wp_get_attachment_image_src( $img->id, 'medium');
$ratio = $img_src[1] / $img_src[2];
if ($ratio > 1.3 and $ratio < 1.6) { // make sure that the image isn't too big, and that it has been scaled by WordPress if ($img_src[1] > 250 or preg_match("/[0-9]+x[0-9]+/", $img_src[0]) == 0)
continue;
if ($img1=='') {
$img1=$img_src[0];
} elseif ($img2=='') {
$img2=$img_src[0];
break;
}
}
}
return (array($img1,$img2));
}

]]>
1154
Site redesign https://robfelty.com/2010/12/01/website-redesign https://robfelty.com/2010/12/01/website-redesign#comments Wed, 01 Dec 2010 16:50:39 +0000 http://robfelty.com/?p=848 robfelty.com 2.0
robfelty.com 2.0

I started my website in 2003. At the time it was hosted by the University of Michigan, where I was a graduate student. They gave all students some space for a personal website. It was really great, though it did come with some limitations, like no php or cgi allowed. I managed to kludge some server side includes and javascript together to get a fairly decent food website. I also had some other stuff on my site like some academic info. In 2006 I started a blog, and started learning wordpress. Since then, most of the new content on my site has been in my blog, and the rest of the site has kind of just been sitting there. I finally decided to try to integrate it all, for several reasons:

  1. Consistent style for all pages
  2. Get everything in a database (for better searching among other things)
  3. Learn some new technology

For that last reason, I had started to look at learning more about drupal, since I have seen a number of jobs looking for drupal developers recently. I spent a week or so looking at that in June, and got a little bit excited about a recipe module I found. However, I decided that I wasn’t crazy about drupal. I think WordPress is miles ahead of drupal in many ways. I was very surprised to find out that uploading pictures is not part of the core of drupal, but rather in a module (and not a very good one at that). Of course, up until WordPress 3.0, WP was mostly just a blogging platform. No longer! With version 3.0, WordPress can easily be used as a full-fledged content management system. So I decided to integrate the disparate portions of my website all into WordPress. During the conversion process I got to learn more about the new features in WordPress 3.0, especially custom post types.

robfelty.com 1.0 - food
robfelty.com 1.0 - food

The biggest challenge of the conversion was converting my recipes. Most of the old pages on my site simply became a WordPress page, but the recipes clearly needed special treatment. I thought I might use a custom post type for recipes, but after a little searching around, I quickly stumbled upon RecipePress, which basically does everything I was looking for (and does in fact use custom post types). RecipePress will work out of the box with any theme, but, upon my request, it also will let you use your own custom theme, which is the route I chose. One small dilemma I faced was that I had multiple pictures in some of my recipes, but I could not insert them into the post content, since RecipePress uses that for the recipe directions. Instead, what I ended up doing was using one image as a featured image, and then I wrote some custom code to display any additional images below the ingredients and contents, like so:


ID );
if (count($pics)>1): ?>


As I started to figure out how I wanted the navigation for the site to work, I also realized that I wanted to update some of the WordPress navigation plugins I have written – Collapsing Categories, Collapsing Archives, and Collapsing Pages. I added support for custom post types for the first two, and added an option for the pages plugin to only list subpages of the current page (this is how the navigation works for the academic, web design, and wordpress plugins portions of the new site). These features are all in the development versions of the respective plugins right now, but I should put out a new stable release soon.

While I was working on importing content from the old site, I also decided to make a new theme as well. I decided to try out the new child theme functionality in WordPress 3.0. It turned out to be quite easy. The only slightly confusing thing for me was how to disable some of the functionality of the parent theme. There are hints on how to to this in the TwentyTen functions.php file, but it took me awhile to actually figure it out. For example, I did not like how TwentyTen handles excerpts, so I wanted to remove the filters for the the except which TwentyTen adds. The way that WordPress handles child themes is that it first looks for the functions.php file in the child theme, and processes it all. Then it looks at the parent theme’s functions.php file and processes that. This means that if you try to simply remove a filter from the parent theme in your child theme, it won’t work, since the child theme is processed first. Instead, you have to do all of this in a function which is called in the after_setup_theme hook. Here is what I used in child theme functions.php file:


function robfelty_child_theme_setup() {
// We are providing our own filter for excerpt_length (or using the unfiltered value)
remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
remove_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
}
add_action( 'after_setup_theme', 'robfelty_child_theme_setup' );

I also added in some CSS3 features, including rounded corners with border-radius, and gradients as well. As always, it is still a work in progress. Feedback is welcome.

]]>
https://robfelty.com/2010/12/01/website-redesign/feed 2 848
New WordPress plugin – Image Browser https://robfelty.com/2010/08/04/new-wordpress-plugin-image-browser Wed, 04 Aug 2010 21:55:03 +0000 http://robfelty.com/?p=687 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.

]]>
687
Showing total number of replies in bbpress https://robfelty.com/2010/04/09/showing-total-number-of-replies-in-bbpress Fri, 09 Apr 2010 21:36:59 +0000 http://robfelty.com/?p=662 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:

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

To get the total number of topics started, use:

get_var("SELECT COUNT(topic_id) FROM " .
$bbdb->prefix . "topics WHERE topic_poster=$user->ID");
?>

]]>
662
WordPress 2.9 image changes https://robfelty.com/2010/01/02/wordpress-2-9-image-changes https://robfelty.com/2010/01/02/wordpress-2-9-image-changes#comments Sat, 02 Jan 2010 22:58:48 +0000 http://robfelty.com/?p=534 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;

]]>
https://robfelty.com/2010/01/02/wordpress-2-9-image-changes/feed 2 534
Releasing the Collapsing Archives WordPress Plugin https://robfelty.com/2007/12/20/releasing-the-collapsing-archives-wordpress-plugin https://robfelty.com/2007/12/20/releasing-the-collapsing-archives-wordpress-plugin#comments Thu, 20 Dec 2007 05:14:09 +0000 http://robfelty.com/2007/12/20/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.

]]>
https://robfelty.com/2007/12/20/releasing-the-collapsing-archives-wordpress-plugin/feed 46 69
Releasing the Collapsing Categories WordPress Plugin https://robfelty.com/2007/12/19/releasing-the-collapsing-categories-wordpress-plugin https://robfelty.com/2007/12/19/releasing-the-collapsing-categories-wordpress-plugin#comments Thu, 20 Dec 2007 03:51:05 +0000 http://robfelty.com/2007/12/19/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.

]]>
https://robfelty.com/2007/12/19/releasing-the-collapsing-categories-wordpress-plugin/feed 84 68
Chronologically ordered blog in wordpress https://robfelty.com/2007/05/14/chronologically-ordered-blog-in-wordpress https://robfelty.com/2007/05/14/chronologically-ordered-blog-in-wordpress#comments Mon, 14 May 2007 15:34:49 +0000 http://robfelty.com/2007/05/14/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:


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

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

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!

]]>
https://robfelty.com/2007/05/14/chronologically-ordered-blog-in-wordpress/feed 9 55