Random featured images in wordpress

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));
}
Posted in php, sql, wordpress | Leave a comment

Postie 1.4.2 released

post to your blog via e-mail Yesterday I released Postie version 1.4.2, the WordPress plugin which gives you advanced features to post to your blog via e-mail. I haven’t done much work on Postie in the last several months, so I finally just decided to call the development version stable, since lots of people have been using it.


Improvements over 1.4.1 include:

  • Fixed mailto link bug (thanks to Jason McNeil)
  • Fixed bug with attachments with non-ascii characters in filename (thanks to
    mtakada)
  • checking for socket errors when checking mail (thanks elysian)
  • fixed issue with multiple files not being inserted correctly
  • Added support for ISO 8859-15 (thanks paolog)
  • fixed sql injection problem (thanks Jose P. Espinal for pointing it out)
  • Fixed namespace clashing for get_config function

Please report any bugs in the postie forum

Posted in wordpress | Tagged , | 6 Comments

Site redesign

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:

<?php  /* additional images here */
$pics =
get_children('post_type=attachment&post_mime_type=image&post_parent=' .
$post->ID );
if (count($pics)>1): ?>
  <div class='gallery'>
  <h4>Additional pictures</h4>
  <?php foreach ($pics as $image): ?>
    <?php if ($image->ID!=$thumbnail_id): ?>
      <dl class='gallery-item'>
        <dt class='gallery-icon'>
          <a href='<?php echo  wp_get_attachment_url($image->ID)?>'>
             <?php echo wp_get_attachment_image($image->ID, 'medium')?>
          </a>
        </dt>
          <dd class='gallery-caption'>
          <?php echo $image->post_excerpt?>
          </dd>
      </dl>
    <?php endif ?>
  <?php endforeach ?>
  </div>
<?php endif ?>

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.

Posted in (x)html, css, php, sql, wordpress | 2 Comments

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.

Posted in photography, php, sql, wordpress | Leave a comment

New wordpress plugin – Image Meta

Image meta options

Image meta options

I just released my 7th wordpress plugin, Image Meta. This one has to do with image handling. Before wordpress 3.0, when you uploaded an image, the filename of the image was set as the title, and if you had given the image a caption (e.g. with Picasa), then the IPTC caption would be used as the caption for the image. In wordpress 3.0, the default is now to use the caption for the title of the image. I don’t like this, mostly because I had set up my theme’s image handling based on the old way. So I decided to write a little plugin which hooks into the image uploading process, and sets all four image fields at once – the title, alt text, caption, and description. The plugin allows you to select between setting each field to either the filename, the IPTC caption, or leaving it blank. I hope some others find it useful.

Posted in photography, wordpress | 2 Comments